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.

158 lines
4.7 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: pwutil.h
  4. //
  5. // Module: CMDIAL32.DLL, CMCFG32.DLL, AND MIGRATE.DLL
  6. //
  7. // Synopsis: Header for pwutil functions
  8. // Simple encryption functions borrowed from RAS
  9. //
  10. // Copyright (c) 1994-1999 Microsoft Corporation
  11. //
  12. // Author: nickball Created 08/03/99
  13. //
  14. //+----------------------------------------------------------------------------
  15. #ifndef CM_PWUTIL_H_
  16. #define CM_PWUTIL_H_
  17. VOID
  18. CmDecodePasswordA(
  19. CHAR* pszPassword
  20. );
  21. VOID
  22. CmDecodePasswordW(
  23. WCHAR* pszPassword
  24. );
  25. VOID
  26. CmEncodePasswordA(
  27. CHAR* pszPassword
  28. );
  29. VOID
  30. CmEncodePasswordW(
  31. WCHAR* pszPassword
  32. );
  33. VOID
  34. CmWipePasswordA(
  35. CHAR* pszPassword
  36. );
  37. VOID
  38. CmWipePasswordW(
  39. WCHAR* pszPassword
  40. );
  41. PVOID CmSecureZeroMemory(IN PVOID ptr, IN SIZE_T cnt);
  42. #ifdef UNICODE
  43. #define CmDecodePassword CmDecodePasswordW
  44. #define CmEncodePassword CmEncodePasswordW
  45. #define CmWipePassword CmWipePasswordW
  46. #else
  47. #define CmDecodePassword CmDecodePasswordA
  48. #define CmEncodePassword CmEncodePasswordA
  49. #define CmWipePassword CmWipePasswordA
  50. #endif
  51. #ifdef _ICM_INC // Only include this code in cmdial32.dll
  52. #include "dynamiclib.h"
  53. #include <wincrypt.h>
  54. #include <cmutil.h>
  55. #include "pwd_str.h"
  56. typedef BOOL (WINAPI *fnCryptProtectDataFunc)(DATA_BLOB*, LPCWSTR, DATA_BLOB*, PVOID, CRYPTPROTECT_PROMPTSTRUCT*, DWORD, DATA_BLOB*);
  57. typedef BOOL (WINAPI *fnCryptUnprotectDataFunc)(DATA_BLOB*, LPWSTR*, DATA_BLOB*, PVOID, CRYPTPROTECT_PROMPTSTRUCT*, DWORD, DATA_BLOB*);
  58. //+----------------------------------------------------------------------------
  59. // Class: CSecurePassword
  60. //
  61. // Synopsis: Manages secrets (passwords) in memory. Because CM runs on Win9x,
  62. // NT4, Win2K, WinXP & .NET Server platform we need to handle
  63. // secrets differently on different platforms. On Win2K+
  64. // this class uses CryptProtectData and CryptUnprotectData. On any
  65. // platform below Win2K there APIs are not supported thus CM
  66. // just uses the old way (not very secure) of XORing passwords in
  67. // memory.
  68. //
  69. // If a caller gets a password from this class (GetPasswordWithAlloc)
  70. // in clear text, that memory needs to be freed by this class by
  71. // calling ClearAndFree. The caller will get an assert upon
  72. // destruction of this class if the caller doesn't use this
  73. // class to free the memory.
  74. //
  75. // This class can protect & unprotect strings of length 0.
  76. //
  77. // Arguments: none
  78. //
  79. // Returns: Nothing
  80. //
  81. // History: 11/05/2002 tomkel Created
  82. //
  83. //+----------------------------------------------------------------------------
  84. class CSecurePassword
  85. {
  86. public:
  87. CSecurePassword();
  88. ~CSecurePassword();
  89. BOOL SetPassword(IN LPWSTR szPassword);
  90. BOOL GetPasswordWithAlloc(OUT LPWSTR* pszClearPw, OUT DWORD* cbClearPw);
  91. VOID ClearAndFree(IN OUT LPWSTR* pszClearPw, IN DWORD cbClearPw);
  92. VOID Init();
  93. VOID UnInit();
  94. BOOL IsEmptyString();
  95. BOOL IsHandleToPassword();
  96. VOID SetMaxDataLenToProtect(DWORD dwMaxDataLen);
  97. DWORD GetMaxDataLenToProtect();
  98. private:
  99. VOID ClearMemberVars();
  100. VOID FreePassword(IN DATA_BLOB *pDBPassword);
  101. BOOL LoadCrypt32AndGetFuncPtrs();
  102. VOID UnloadCrypt32();
  103. DWORD DecodePassword(IN DATA_BLOB * pDataBlobPassword,
  104. OUT DWORD * pcbPassword,
  105. OUT PBYTE * ppbPassword);
  106. DWORD EncodePassword(IN DWORD cbPassword,
  107. IN PBYTE pbPassword,
  108. OUT DATA_BLOB * pDataBlobPassword);
  109. //
  110. // Member variables
  111. //
  112. DATA_BLOB* m_pEncryptedPW; // Encrypted PW used in Win2K+
  113. TCHAR m_tszPassword[PWLEN+1]; // password (used downlevel - Win9x & NT4)
  114. CDynamicLibrary m_dllCrypt32;
  115. fnCryptProtectDataFunc fnCryptProtectData;
  116. fnCryptUnprotectDataFunc fnCryptUnprotectData;
  117. BOOL m_fIsLibAndFuncPtrsAvail;
  118. BOOL m_fIsEmptyString;
  119. BOOL m_fIsHandleToPassword; // When users sets 16 *s, this will be TRUE
  120. DWORD m_dwMaxDataLen;
  121. // Used for debugging. At destruction time this needs to be 0.
  122. // Each call to GetPasswordWithAlloc increments this
  123. // Each call to ClearAndFree decrements this.
  124. int m_iAllocAndFreeCounter;
  125. }; //class CSecurePassword
  126. #endif // _ICM_INC
  127. #endif // CM_PWUTIL_H_