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.

145 lines
4.0 KiB

  1. #include "stdafx.h"
  2. #include "common.h"
  3. #include <wincrypt.h>
  4. #include <strsafe.h>
  5. #ifdef _DEBUG
  6. #undef THIS_FILE
  7. static char BASED_CODE THIS_FILE[] = __FILE__;
  8. #endif
  9. #define new DEBUG_NEW
  10. // Given a clear text password. this function will encrypt the password in memory
  11. // then allocate memory and passback the encrypted password into the memory
  12. //
  13. // The class can then store the ppszEncryptedPassword in it's member variables safely
  14. // and when the class is destructed, should SecureZeroMemory() out the password and LocalFree() up the the memory.
  15. HRESULT EncryptMemoryPassword(LPWSTR pszClearTextPassword,LPWSTR *ppszEncryptedPassword,DWORD *ppdwBufferBytes)
  16. {
  17. HRESULT hRes = E_FAIL;
  18. LPWSTR pszTempStr = NULL;
  19. DWORD dwTempStrSizeOf = 0;
  20. *ppszEncryptedPassword = NULL;
  21. *ppdwBufferBytes = 0;
  22. if (pszClearTextPassword)
  23. {
  24. // We should check if the pszClearTextPassword is null terminated before doing wcslen
  25. DWORD dwBufferByteLen = (wcslen(pszClearTextPassword) + 1) * sizeof(WCHAR);
  26. if (CRYPTPROTECTMEMORY_BLOCK_SIZE > 0 && dwBufferByteLen > 0)
  27. {
  28. int iBlocks = dwBufferByteLen / CRYPTPROTECTMEMORY_BLOCK_SIZE;
  29. iBlocks++;
  30. dwTempStrSizeOf = iBlocks * CRYPTPROTECTMEMORY_BLOCK_SIZE;
  31. pszTempStr = (LPWSTR) LocalAlloc(LPTR,dwTempStrSizeOf);
  32. if (!pszTempStr)
  33. {
  34. hRes = E_OUTOFMEMORY;
  35. goto EncryptMemoryPassword_Exit;
  36. }
  37. ZeroMemory(pszTempStr,dwTempStrSizeOf);
  38. StringCbCopy(pszTempStr,dwTempStrSizeOf,pszClearTextPassword);
  39. if (FALSE != CryptProtectMemory(pszTempStr,dwTempStrSizeOf,CRYPTPROTECTMEMORY_SAME_PROCESS))
  40. {
  41. // We're all set...
  42. *ppszEncryptedPassword = pszTempStr;
  43. *ppdwBufferBytes = dwTempStrSizeOf;
  44. hRes = S_OK;
  45. goto EncryptMemoryPassword_Exit;
  46. }
  47. }
  48. }
  49. EncryptMemoryPassword_Exit:
  50. if (FAILED(hRes))
  51. {
  52. if (pszTempStr)
  53. {
  54. if (dwTempStrSizeOf > 0)
  55. {
  56. SecureZeroMemory(pszTempStr,dwTempStrSizeOf);
  57. }
  58. LocalFree(pszTempStr);
  59. pszTempStr = NULL;
  60. dwTempStrSizeOf = 0;
  61. }
  62. }
  63. return hRes;
  64. }
  65. // Given a encrypted password (encrypted in the same process with EncryptMemoryPassword -- which uses CryptProtectMemory)
  66. // this function will allocate some new memory, decrypt the password and put it in the new memory
  67. // and return it back to the caller in ppszReturnedPassword.
  68. //
  69. // The caller must ensure to erase and free the memory after it is finished using the decrypted password.
  70. //
  71. // LPWSTR lpwstrTempPassword = NULL;
  72. //
  73. // if (FAILED(DecryptMemoryPassword((LPWSTR) pszUserPasswordEncrypted,&lpwstrTempPassword,cbUserPasswordEncrypted)))
  74. // {
  75. // // do some failure processing...
  76. // }
  77. //
  78. // // use password for whatever you needed to use it for...
  79. //
  80. // if (lpwstrTempPassword)
  81. // {
  82. // if (cbTempPassword > 0)
  83. // (
  84. // SecureZeroMemory(lpwstrTempPassword,cbTempPassword);
  85. // )
  86. // LocalFree(lpwstrTempPassword);lpwstrTempPassword = NULL;
  87. // }
  88. HRESULT DecryptMemoryPassword(LPWSTR pszEncodedPassword,LPWSTR *ppszReturnedPassword,DWORD dwBufferBytes)
  89. {
  90. HRESULT hRes = E_FAIL;
  91. LPWSTR pszTempStr = NULL;
  92. if (!dwBufferBytes || !ppszReturnedPassword)
  93. {
  94. return E_FAIL;
  95. }
  96. *ppszReturnedPassword = NULL;
  97. if (dwBufferBytes)
  98. {
  99. pszTempStr = (LPWSTR) LocalAlloc(LPTR,dwBufferBytes);
  100. if (!pszTempStr)
  101. {
  102. hRes = E_OUTOFMEMORY;
  103. goto DecryptMemoryPassword_Exit;
  104. }
  105. ZeroMemory(pszTempStr,dwBufferBytes);
  106. memcpy(pszTempStr,pszEncodedPassword,dwBufferBytes);
  107. if (FALSE != CryptUnprotectMemory(pszTempStr,dwBufferBytes,CRYPTPROTECTMEMORY_SAME_PROCESS))
  108. {
  109. // We're all set...
  110. *ppszReturnedPassword = pszTempStr;
  111. hRes = S_OK;
  112. }
  113. }
  114. DecryptMemoryPassword_Exit:
  115. if (FAILED(hRes))
  116. {
  117. if (pszTempStr)
  118. {
  119. if (dwBufferBytes > 0)
  120. {
  121. SecureZeroMemory(pszTempStr,dwBufferBytes);
  122. }
  123. LocalFree(pszTempStr);
  124. pszTempStr = NULL;
  125. }
  126. }
  127. return hRes;
  128. }