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.

224 lines
4.2 KiB

  1. /* Copyright (c) 1994, Microsoft Corporation, all rights reserved
  2. **
  3. ** pwutil.c
  4. ** Remote Access
  5. ** Password handling routines
  6. **
  7. ** 03/01/94 Steve Cobb
  8. */
  9. #include <windows.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define INCL_PWUTIL
  13. #include <ppputil.h>
  14. #define PASSWORDMAGIC 0xA5
  15. VOID ReverseString( CHAR* psz );
  16. CHAR*
  17. DecodePw(
  18. IN CHAR chSeed,
  19. IN OUT CHAR* pszPassword )
  20. /* Un-obfuscate 'pszPassword' in place.
  21. **
  22. ** Returns the address of 'pszPassword'.
  23. */
  24. {
  25. return EncodePw( chSeed, pszPassword );
  26. }
  27. CHAR*
  28. EncodePw(
  29. IN CHAR chSeed,
  30. IN OUT CHAR* pszPassword )
  31. /* Obfuscate 'pszPassword' in place to foil memory scans for passwords.
  32. **
  33. ** Returns the address of 'pszPassword'.
  34. */
  35. {
  36. if (pszPassword)
  37. {
  38. CHAR* psz;
  39. ReverseString( pszPassword );
  40. for (psz = pszPassword; *psz != '\0'; ++psz)
  41. {
  42. if (*psz != chSeed)
  43. *psz ^= chSeed;
  44. /*
  45. if (*psz != (CHAR)PASSWORDMAGIC)
  46. *psz ^= PASSWORDMAGIC;
  47. */
  48. }
  49. }
  50. return pszPassword;
  51. }
  52. VOID
  53. ReverseString(
  54. CHAR* psz )
  55. /* Reverses order of characters in 'psz'.
  56. */
  57. {
  58. CHAR* pszBegin;
  59. CHAR* pszEnd;
  60. for (pszBegin = psz, pszEnd = psz + strlen( psz ) - 1;
  61. pszBegin < pszEnd;
  62. ++pszBegin, --pszEnd)
  63. {
  64. CHAR ch = *pszBegin;
  65. *pszBegin = *pszEnd;
  66. *pszEnd = ch;
  67. }
  68. }
  69. CHAR*
  70. WipePw(
  71. IN OUT CHAR* pszPassword )
  72. /* Zero out the memory occupied by a password.
  73. **
  74. ** Returns the address of 'pszPassword'.
  75. */
  76. {
  77. if (pszPassword)
  78. {
  79. CHAR* psz = pszPassword;
  80. while (*psz != '\0')
  81. *psz++ = '\0';
  82. }
  83. return pszPassword;
  84. }
  85. DWORD
  86. EncodePassword(
  87. DWORD cbPassword,
  88. PBYTE pbPassword,
  89. DATA_BLOB * pDataBlobPassword)
  90. {
  91. DWORD dwErr = NO_ERROR;
  92. DATA_BLOB DataBlobIn;
  93. if(NULL == pDataBlobPassword)
  94. {
  95. dwErr = E_INVALIDARG;
  96. goto done;
  97. }
  98. if( (0 == cbPassword)
  99. || (NULL == pbPassword))
  100. {
  101. //
  102. // nothing to encrypt. just return success
  103. //
  104. goto done;
  105. }
  106. ZeroMemory(pDataBlobPassword, sizeof(DATA_BLOB));
  107. DataBlobIn.cbData = cbPassword;
  108. DataBlobIn.pbData = pbPassword;
  109. if(!CryptProtectData(
  110. &DataBlobIn,
  111. NULL,
  112. NULL,
  113. NULL,
  114. NULL,
  115. CRYPTPROTECT_UI_FORBIDDEN |
  116. CRYPTPROTECT_LOCAL_MACHINE,
  117. pDataBlobPassword))
  118. {
  119. dwErr = GetLastError();
  120. goto done;
  121. }
  122. done:
  123. return dwErr;
  124. }
  125. DWORD
  126. DecodePassword(
  127. DATA_BLOB * pDataBlobPassword,
  128. DWORD * pcbPassword,
  129. PBYTE * ppbPassword)
  130. {
  131. DWORD dwErr = NO_ERROR;
  132. DATA_BLOB DataOut;
  133. if( (NULL == pDataBlobPassword)
  134. || (NULL == pcbPassword)
  135. || (NULL == ppbPassword))
  136. {
  137. dwErr = E_INVALIDARG;
  138. goto done;
  139. }
  140. *pcbPassword = 0;
  141. *ppbPassword = NULL;
  142. if( (NULL == pDataBlobPassword->pbData)
  143. || (0 == pDataBlobPassword->cbData))
  144. {
  145. //
  146. // nothing to decrypt. Just return success.
  147. //
  148. goto done;
  149. }
  150. ZeroMemory(&DataOut, sizeof(DATA_BLOB));
  151. if(!CryptUnprotectData(
  152. pDataBlobPassword,
  153. NULL,
  154. NULL,
  155. NULL,
  156. NULL,
  157. CRYPTPROTECT_UI_FORBIDDEN |
  158. CRYPTPROTECT_LOCAL_MACHINE,
  159. &DataOut))
  160. {
  161. dwErr = GetLastError();
  162. goto done;
  163. }
  164. *pcbPassword = DataOut.cbData;
  165. *ppbPassword = DataOut.pbData;
  166. done:
  167. return dwErr;
  168. }
  169. VOID
  170. FreePassword(DATA_BLOB *pDBPassword)
  171. {
  172. if(NULL == pDBPassword)
  173. {
  174. return;
  175. }
  176. if(NULL != pDBPassword->pbData)
  177. {
  178. RtlSecureZeroMemory(pDBPassword->pbData, pDBPassword->cbData);
  179. LocalFree(pDBPassword->pbData);
  180. }
  181. ZeroMemory(pDBPassword, sizeof(DATA_BLOB));
  182. }