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.

348 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 1994-2002 Microsoft Corporation
  3. Module Name :
  4. strpass.cpp
  5. Abstract:
  6. Message Functions
  7. Author:
  8. Aaron Lee (aaronl)
  9. Project:
  10. Internet Services Manager
  11. Revision History:
  12. --*/
  13. #include "stdafx.h"
  14. #include "common.h"
  15. #include "strpass.h"
  16. #include "cryptpass.h"
  17. #include <strsafe.h>
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22. #define new DEBUG_NEW
  23. void CStrPassword::ClearPasswordBuffers(void)
  24. {
  25. if (NULL != m_pszDataEncrypted)
  26. {
  27. if (m_cbDataEncrypted > 0)
  28. {
  29. SecureZeroMemory(m_pszDataEncrypted,m_cbDataEncrypted);
  30. }
  31. LocalFree(m_pszDataEncrypted);m_pszDataEncrypted=NULL;
  32. m_pszDataEncrypted = NULL;
  33. }
  34. m_cbDataEncrypted = 0;
  35. }
  36. // constructor
  37. CStrPassword::CStrPassword()
  38. {
  39. m_pszDataEncrypted = NULL;
  40. m_cbDataEncrypted = 0;
  41. }
  42. CStrPassword::~CStrPassword()
  43. {
  44. ClearPasswordBuffers();
  45. }
  46. // constructor
  47. CStrPassword::CStrPassword(LPTSTR lpch)
  48. {
  49. m_pszDataEncrypted = NULL;
  50. m_cbDataEncrypted = 0;
  51. // Copy the string
  52. if (NULL != lpch)
  53. {
  54. if (FAILED(EncryptMemoryPassword(lpch,&m_pszDataEncrypted,&m_cbDataEncrypted)))
  55. {
  56. ASSERT(FALSE);
  57. }
  58. }
  59. }
  60. // constructor
  61. CStrPassword::CStrPassword(LPCTSTR lpch)
  62. {
  63. CStrPassword((LPTSTR) lpch);
  64. }
  65. // constructor
  66. CStrPassword::CStrPassword(CStrPassword& csPassword)
  67. {
  68. m_pszDataEncrypted = NULL;
  69. m_cbDataEncrypted = 0;
  70. LPTSTR lpTempPassword = csPassword.GetClearTextPassword();
  71. if (FAILED(EncryptMemoryPassword((LPTSTR) lpTempPassword,&m_pszDataEncrypted,&m_cbDataEncrypted)))
  72. {
  73. ASSERT(FALSE);
  74. }
  75. csPassword.DestroyClearTextPassword(lpTempPassword);
  76. }
  77. BOOL CStrPassword::IsEmpty() const
  78. {
  79. if (m_pszDataEncrypted && (m_cbDataEncrypted > 0))
  80. {
  81. return FALSE;
  82. }
  83. return TRUE;
  84. }
  85. void CStrPassword::Empty()
  86. {
  87. ClearPasswordBuffers();
  88. }
  89. int CStrPassword::GetLength() const
  90. {
  91. int iRet = 0;
  92. LPTSTR lpszTempPassword = NULL;
  93. if (m_pszDataEncrypted && (m_cbDataEncrypted > 0))
  94. {
  95. if (SUCCEEDED(DecryptMemoryPassword((LPTSTR) m_pszDataEncrypted,&lpszTempPassword,m_cbDataEncrypted)))
  96. {
  97. iRet = _tcslen(lpszTempPassword);
  98. }
  99. }
  100. if (lpszTempPassword)
  101. {
  102. SecureZeroMemory(lpszTempPassword,(_tcslen(lpszTempPassword)+1) * sizeof(TCHAR));
  103. LocalFree(lpszTempPassword);lpszTempPassword=NULL;
  104. }
  105. return iRet;
  106. };
  107. int CStrPassword::GetByteLength() const
  108. {
  109. int iRet = 0;
  110. LPTSTR lpszTempPassword = NULL;
  111. if (m_pszDataEncrypted && (m_cbDataEncrypted > 0))
  112. {
  113. if (SUCCEEDED(DecryptMemoryPassword((LPTSTR) m_pszDataEncrypted,&lpszTempPassword,m_cbDataEncrypted)))
  114. {
  115. iRet = (_tcslen(lpszTempPassword) + 1) * sizeof(TCHAR);
  116. }
  117. }
  118. if (lpszTempPassword)
  119. {
  120. SecureZeroMemory(lpszTempPassword,(_tcslen(lpszTempPassword)+1) * sizeof(TCHAR));
  121. LocalFree(lpszTempPassword);lpszTempPassword=NULL;
  122. }
  123. return iRet;
  124. };
  125. int CStrPassword::Compare(LPCTSTR lpsz) const
  126. {
  127. // identical = 0
  128. // not equal = 1
  129. int iRet = 1;
  130. LPTSTR lpszTempPassword = NULL;
  131. if (lpsz == NULL)
  132. {
  133. return this->IsEmpty() ? 0 : 1;
  134. }
  135. if (lpsz[0] == NULL)
  136. {
  137. return this->IsEmpty() ? 0 : 1;
  138. }
  139. // Decrypt what we have
  140. if (!m_pszDataEncrypted || (m_cbDataEncrypted < 1))
  141. {
  142. // means we have nothing in here
  143. // but they want to compare it to something
  144. return iRet;
  145. }
  146. if (FAILED(DecryptMemoryPassword((LPTSTR) m_pszDataEncrypted,&lpszTempPassword,m_cbDataEncrypted)))
  147. {
  148. goto CStrPassword_Compare_Exit;
  149. }
  150. else
  151. {
  152. iRet = _tcscmp(lpszTempPassword,lpsz);
  153. }
  154. CStrPassword_Compare_Exit:
  155. if (lpszTempPassword)
  156. {
  157. LocalFree(lpszTempPassword);lpszTempPassword=NULL;
  158. }
  159. return iRet;
  160. }
  161. const CStrPassword& CStrPassword::operator=(LPCTSTR lpsz)
  162. {
  163. ClearPasswordBuffers();
  164. if (lpsz != NULL)
  165. {
  166. // make sure it's pointing to some value
  167. if (*lpsz != NULL)
  168. {
  169. // Copy the string
  170. if (FAILED(EncryptMemoryPassword((LPTSTR) lpsz,&m_pszDataEncrypted,&m_cbDataEncrypted)))
  171. {
  172. ASSERT(FALSE);
  173. }
  174. }
  175. }
  176. return *this;
  177. }
  178. const CStrPassword& CStrPassword::operator= (CStrPassword& StrPass)
  179. {
  180. // handle the a = a case.
  181. if (this == &StrPass)
  182. {
  183. return *this;
  184. }
  185. ClearPasswordBuffers();
  186. if (!StrPass.IsEmpty())
  187. {
  188. LPTSTR p = StrPass.GetClearTextPassword();
  189. ASSERT(NULL != p);
  190. if (FAILED(EncryptMemoryPassword((LPTSTR) p,&m_pszDataEncrypted,&m_cbDataEncrypted)))
  191. {
  192. ASSERT(FALSE);
  193. }
  194. StrPass.DestroyClearTextPassword(p);
  195. }
  196. return *this;
  197. }
  198. void CStrPassword::CopyTo(CString& stringSrc)
  199. {
  200. LPTSTR lpTempPassword = GetClearTextPassword();
  201. stringSrc = lpTempPassword;
  202. DestroyClearTextPassword(lpTempPassword);
  203. return;
  204. }
  205. void CStrPassword::CopyTo(CStrPassword& stringSrc)
  206. {
  207. LPTSTR lpTempPassword = GetClearTextPassword();
  208. stringSrc = (LPCTSTR) lpTempPassword;
  209. DestroyClearTextPassword(lpTempPassword);
  210. return;
  211. }
  212. int CStrPassword::Compare(CString& csString) const
  213. {
  214. int iRet = 1;
  215. if (!csString.IsEmpty())
  216. {
  217. return Compare((LPCTSTR) csString);
  218. }
  219. return iRet;
  220. }
  221. int CStrPassword::Compare(CStrPassword& cstrPassword) const
  222. {
  223. int iRet = 1;
  224. if (!cstrPassword.IsEmpty())
  225. {
  226. LPTSTR lpTempPassword = cstrPassword.GetClearTextPassword();
  227. iRet = Compare((LPCTSTR) lpTempPassword);
  228. cstrPassword.DestroyClearTextPassword(lpTempPassword);
  229. return iRet;
  230. }
  231. return iRet;
  232. }
  233. // user needs to LocalFree return.
  234. // or call DestroyClearTextPassword.
  235. LPTSTR CStrPassword::GetClearTextPassword()
  236. {
  237. LPTSTR lpszTempPassword = NULL;
  238. if (m_pszDataEncrypted && (m_cbDataEncrypted > 0))
  239. {
  240. if (FAILED(DecryptMemoryPassword((LPTSTR) m_pszDataEncrypted,&lpszTempPassword,m_cbDataEncrypted)))
  241. {
  242. if (lpszTempPassword)
  243. {
  244. LocalFree(lpszTempPassword);lpszTempPassword=NULL;
  245. }
  246. }
  247. else
  248. {
  249. return lpszTempPassword;
  250. }
  251. }
  252. return NULL;
  253. }
  254. void CStrPassword::DestroyClearTextPassword(LPTSTR lpClearTextPassword) const
  255. {
  256. if (lpClearTextPassword)
  257. {
  258. SecureZeroMemory(lpClearTextPassword,(_tcslen(lpClearTextPassword)+1) * sizeof(TCHAR));
  259. LocalFree(lpClearTextPassword);lpClearTextPassword=NULL;
  260. }
  261. return;
  262. }
  263. // assign to a CString
  264. CStrPassword::operator CString()
  265. {
  266. LPTSTR lpTempPassword = GetClearTextPassword();
  267. CString csTempCString(lpTempPassword);
  268. DestroyClearTextPassword(lpTempPassword);
  269. return csTempCString;
  270. }
  271. bool CStrPassword::operator==(CStrPassword& csCompareToMe)
  272. {
  273. LPTSTR lpTempPassword1 = NULL;
  274. LPTSTR lpTempPassword2 = NULL;
  275. bool result = FALSE;
  276. // handle the a == a case
  277. if (this == &csCompareToMe)
  278. {
  279. return TRUE;
  280. }
  281. if (GetLength() != csCompareToMe.GetLength())
  282. {
  283. // can't be the same if lengths differ...
  284. return FALSE;
  285. }
  286. // check the case when both are empty (fix for 593488)
  287. if (GetLength() == 0 && csCompareToMe.GetLength() == 0)
  288. {
  289. return TRUE;
  290. }
  291. // Two strings are the same if their decoded contents are the same.
  292. lpTempPassword1 = GetClearTextPassword();
  293. lpTempPassword2 = csCompareToMe.GetClearTextPassword();
  294. result = (_tcscmp(lpTempPassword1, lpTempPassword2) == 0);
  295. if (lpTempPassword1)
  296. {DestroyClearTextPassword(lpTempPassword1);}
  297. if (lpTempPassword2)
  298. {csCompareToMe.DestroyClearTextPassword(lpTempPassword2);}
  299. return result;
  300. }