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.

339 lines
7.8 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. }
  33. m_pszDataEncrypted = NULL;
  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. // Copy the string
  167. if (FAILED(EncryptMemoryPassword((LPTSTR) lpsz,&m_pszDataEncrypted,&m_cbDataEncrypted)))
  168. {
  169. ASSERT(FALSE);
  170. }
  171. }
  172. return *this;
  173. }
  174. const CStrPassword& CStrPassword::operator= (CStrPassword& lpStrPass)
  175. {
  176. // handle the a = a case.
  177. if (this == &lpStrPass)
  178. {
  179. return *this;
  180. }
  181. ClearPasswordBuffers();
  182. LPTSTR lpTempPassword = lpStrPass.GetClearTextPassword();
  183. if (FAILED(EncryptMemoryPassword((LPTSTR) lpTempPassword,&m_pszDataEncrypted,&m_cbDataEncrypted)))
  184. {
  185. ASSERT(FALSE);
  186. }
  187. lpStrPass.DestroyClearTextPassword(lpTempPassword);
  188. return *this;
  189. }
  190. void CStrPassword::CopyTo(CString& stringSrc)
  191. {
  192. LPTSTR lpTempPassword = GetClearTextPassword();
  193. stringSrc = lpTempPassword;
  194. DestroyClearTextPassword(lpTempPassword);
  195. return;
  196. }
  197. void CStrPassword::CopyTo(CStrPassword& stringSrc)
  198. {
  199. LPTSTR lpTempPassword = GetClearTextPassword();
  200. stringSrc = (LPCTSTR) lpTempPassword;
  201. DestroyClearTextPassword(lpTempPassword);
  202. return;
  203. }
  204. int CStrPassword::Compare(CString& csString) const
  205. {
  206. int iRet = 1;
  207. if (!csString.IsEmpty())
  208. {
  209. return Compare((LPCTSTR) csString);
  210. }
  211. return iRet;
  212. }
  213. int CStrPassword::Compare(CStrPassword& cstrPassword) const
  214. {
  215. int iRet = 1;
  216. if (!cstrPassword.IsEmpty())
  217. {
  218. LPTSTR lpTempPassword = cstrPassword.GetClearTextPassword();
  219. iRet = Compare((LPCTSTR) lpTempPassword);
  220. cstrPassword.DestroyClearTextPassword(lpTempPassword);
  221. return iRet;
  222. }
  223. return iRet;
  224. }
  225. // user needs to LocalFree return.
  226. // or call DestroyClearTextPassword.
  227. LPTSTR CStrPassword::GetClearTextPassword()
  228. {
  229. LPTSTR lpszTempPassword = NULL;
  230. if (m_pszDataEncrypted && (m_cbDataEncrypted > 0))
  231. {
  232. if (FAILED(DecryptMemoryPassword((LPTSTR) m_pszDataEncrypted,&lpszTempPassword,m_cbDataEncrypted)))
  233. {
  234. if (lpszTempPassword)
  235. {
  236. LocalFree(lpszTempPassword);lpszTempPassword=NULL;
  237. }
  238. }
  239. else
  240. {
  241. return lpszTempPassword;
  242. }
  243. }
  244. return NULL;
  245. }
  246. void CStrPassword::DestroyClearTextPassword(LPTSTR lpClearTextPassword) const
  247. {
  248. if (lpClearTextPassword)
  249. {
  250. SecureZeroMemory(lpClearTextPassword,(_tcslen(lpClearTextPassword)+1) * sizeof(TCHAR));
  251. LocalFree(lpClearTextPassword);lpClearTextPassword=NULL;
  252. }
  253. return;
  254. }
  255. // assign to a CString
  256. CStrPassword::operator CString()
  257. {
  258. CString csTempCString;
  259. LPTSTR lpTempPassword = GetClearTextPassword();
  260. if (lpTempPassword)
  261. {
  262. csTempCString = lpTempPassword;
  263. DestroyClearTextPassword(lpTempPassword);
  264. }
  265. return csTempCString;
  266. }
  267. bool CStrPassword::operator==(CStrPassword& csCompareToMe)
  268. {
  269. LPTSTR lpTempPassword1 = NULL;
  270. LPTSTR lpTempPassword2 = NULL;
  271. bool result = FALSE;
  272. // handle the a == a case
  273. if (this == &csCompareToMe)
  274. {
  275. return TRUE;
  276. }
  277. if (GetLength() != csCompareToMe.GetLength())
  278. {
  279. // can't be the same if lengths differ...
  280. return FALSE;
  281. }
  282. // Two strings are the same if their decoded contents are the same.
  283. lpTempPassword1 = GetClearTextPassword();
  284. lpTempPassword2 = csCompareToMe.GetClearTextPassword();
  285. result = (_tcscmp(lpTempPassword1, lpTempPassword2) == 0);
  286. if (lpTempPassword1)
  287. {DestroyClearTextPassword(lpTempPassword1);}
  288. if (lpTempPassword2)
  289. {csCompareToMe.DestroyClearTextPassword(lpTempPassword2);}
  290. return result;
  291. }