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.

280 lines
6.4 KiB

  1. #pragma once
  2. #include <TChar.h>
  3. #include <Windows.h>
  4. #include <WinCrypt.h>
  5. #include <ComDef.h>
  6. #define ENCRYPTION_KEY_SIZE 16 // in bytes
  7. #define SESSION_KEY_SIZE 16 // in bytes
  8. #define GET_BYTE_ARRAY_DATA(v) ((char*)((v).parray->pvData))
  9. #define GET_BYTE_ARRAY_SIZE(v) ((unsigned long)((v).parray->rgsabound[0].cElements))
  10. //---------------------------------------------------------------------------
  11. // Crypt Provider Class
  12. //---------------------------------------------------------------------------
  13. class CCryptProvider
  14. {
  15. public:
  16. CCryptProvider();
  17. CCryptProvider(const CCryptProvider& r);
  18. ~CCryptProvider();
  19. CCryptProvider& operator =(const CCryptProvider& r);
  20. HCRYPTHASH CreateHash(ALG_ID aid);
  21. HCRYPTKEY DeriveKey(ALG_ID aid, HCRYPTHASH hHash, DWORD dwFlags = 0);
  22. _variant_t GenerateRandom(DWORD cbData) const;
  23. void GenerateRandom(BYTE* pbData, DWORD cbData) const;
  24. protected:
  25. HCRYPTPROV m_hProvider;
  26. };
  27. //---------------------------------------------------------------------------
  28. // Crypt Key Class
  29. //---------------------------------------------------------------------------
  30. class CCryptKey
  31. {
  32. public:
  33. CCryptKey(HCRYPTKEY hKey = NULL);
  34. ~CCryptKey();
  35. operator HCRYPTKEY()
  36. {
  37. return m_hKey;
  38. }
  39. void Attach(HCRYPTKEY hKey)
  40. {
  41. m_hKey = hKey;
  42. }
  43. HCRYPTKEY Detach()
  44. {
  45. HCRYPTKEY hKey = m_hKey;
  46. m_hKey = NULL;
  47. return hKey;
  48. }
  49. _variant_t Encrypt(HCRYPTHASH hHash, bool bFinal, const _variant_t& vntData);
  50. _variant_t Decrypt(HCRYPTHASH hHash, bool bFinal, const _variant_t& vntData);
  51. protected:
  52. CCryptKey(const CCryptKey& key) {}
  53. CCryptKey& operator =(const CCryptKey& key) { return *this; }
  54. protected:
  55. HCRYPTKEY m_hKey;
  56. };
  57. //---------------------------------------------------------------------------
  58. // Crypt Hash Class
  59. //---------------------------------------------------------------------------
  60. class CCryptHash
  61. {
  62. public:
  63. CCryptHash(HCRYPTHASH hHash = NULL);
  64. ~CCryptHash();
  65. operator HCRYPTHASH()
  66. {
  67. return m_hHash;
  68. }
  69. void Attach(HCRYPTHASH hHash)
  70. {
  71. m_hHash = hHash;
  72. }
  73. HCRYPTKEY Detach()
  74. {
  75. HCRYPTKEY hHash = m_hHash;
  76. m_hHash = NULL;
  77. return hHash;
  78. }
  79. _variant_t GetValue() const;
  80. void SetValue(const _variant_t& vntValue);
  81. void Hash(LPCTSTR pszData);
  82. void Hash(const _variant_t& vntData);
  83. void Hash(BYTE* pbData, DWORD cbData);
  84. bool operator ==(const CCryptHash& hash);
  85. bool operator !=(const CCryptHash& hash)
  86. {
  87. return !this->operator ==(hash);
  88. }
  89. protected:
  90. CCryptHash(const CCryptKey& hash) {}
  91. CCryptHash& operator =(const CCryptHash& hash) { return *this; }
  92. protected:
  93. HCRYPTHASH m_hHash;
  94. };
  95. //---------------------------------------------------------------------------
  96. // Domain Crypt Class
  97. //---------------------------------------------------------------------------
  98. class CDomainCrypt : public CCryptProvider
  99. {
  100. protected:
  101. CDomainCrypt();
  102. ~CDomainCrypt();
  103. HCRYPTKEY GetEncryptionKey(LPCTSTR pszKeyId);
  104. void StoreBytes(LPCTSTR pszId, const _variant_t& vntBytes);
  105. void StoreBytes(LPCTSTR pszId, BYTE* pBytes, DWORD cBytes);
  106. _variant_t RetrieveBytes(LPCTSTR pszId);
  107. protected:
  108. static _TCHAR m_szIdPrefix[];
  109. };
  110. //---------------------------------------------------------------------------
  111. // Target Crypt Class
  112. //
  113. // CreateEncryptionKey
  114. // - creates encryption key
  115. // - stores encryption key using key identifier
  116. // - returns encryption key encrypted with given password
  117. //---------------------------------------------------------------------------
  118. class CTargetCrypt : public CDomainCrypt
  119. {
  120. public:
  121. CTargetCrypt();
  122. ~CTargetCrypt();
  123. _variant_t CreateEncryptionKey(LPCTSTR pszKeyId, LPCTSTR pszPassword = NULL);
  124. _variant_t CreateSession(LPCTSTR pszKeyId);
  125. _variant_t Encrypt(_bstr_t strData);
  126. protected:
  127. void StoreBytes(LPCTSTR pszId, const _variant_t& vntBytes)
  128. {
  129. LPTSTR psz = (LPTSTR) _alloca((_tcslen(m_szIdPrefix) + 1 + _tcslen(pszId) + 1) * sizeof(_TCHAR));
  130. _tcscpy(psz, m_szIdPrefix);
  131. _tcscat(psz, _T("_"));
  132. _tcscat(psz, pszId);
  133. CDomainCrypt::StoreBytes(psz, vntBytes);
  134. }
  135. _variant_t RetrieveBytes(LPCTSTR pszId)
  136. {
  137. LPTSTR psz = (LPTSTR) _alloca((_tcslen(m_szIdPrefix) + 1 + _tcslen(pszId) + 1) * sizeof(_TCHAR));
  138. _tcscpy(psz, m_szIdPrefix);
  139. _tcscat(psz, _T("_"));
  140. _tcscat(psz, pszId);
  141. return CDomainCrypt::RetrieveBytes(psz);
  142. }
  143. protected:
  144. CCryptKey m_keySession;
  145. };
  146. //---------------------------------------------------------------------------
  147. // Source Crypt Class
  148. //---------------------------------------------------------------------------
  149. class CSourceCrypt : public CDomainCrypt
  150. {
  151. public:
  152. CSourceCrypt();
  153. ~CSourceCrypt();
  154. void ImportEncryptionKey(const _variant_t& vntEncryptedKey, LPCTSTR pszPassword = NULL);
  155. void ImportSessionKey(const _variant_t& vntEncryptedKey);
  156. _bstr_t Decrypt(const _variant_t& vntData);
  157. protected:
  158. CCryptKey m_keySession;
  159. };
  160. //---------------------------------------------------------------------------
  161. // Use Cases
  162. //---------------------------------------------------------------------------
  163. //
  164. // Target Domain Controller
  165. // ------------------------
  166. // Generate Encryption Key
  167. // - given source domain name and optional password
  168. // - generate 128 bit encryption key
  169. // - store encryption key using source domain name
  170. // - if given optional password encrypt key with password
  171. // - return encrypted key
  172. //
  173. // Generate Session Key
  174. // - given source domain name
  175. // - generate 128 bit session key
  176. // - generate hash of session key
  177. // - retrieve encryption key using source domain name
  178. // - encrypt session key and hash with encryption key
  179. // - return encrypted session key/hash
  180. //
  181. // Encrypt Data
  182. // - given data
  183. // - encrypt data using session key
  184. // - return encrypted data
  185. //
  186. // Password Export Server (PES)
  187. // ----------------------------
  188. // Store Encryption Key
  189. // - given encrypted encryption key and password
  190. // - decrypt key using password
  191. // - store key
  192. //
  193. // Decrypt Session Key
  194. // - given an encrypted session key / hash
  195. // - decrypt using encryption key
  196. // - generate hash of decrypted session key
  197. // - compare against decrypted hash
  198. // - store session key
  199. // - return success or failure
  200. //
  201. // Decrypt Data
  202. // - given encrypted data
  203. // - decrypt data using session key
  204. // - return un-encrypted data
  205. //---------------------------------------------------------------------------