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.

152 lines
3.2 KiB

  1. /*++
  2. Copyright (c) 2003 Microsoft Corporation
  3. Module Name:
  4. xstring.h
  5. Abstract:
  6. Author:
  7. Stephen A Sulzer (ssulzer) 16-Jan-2003
  8. --*/
  9. //
  10. // class implementation of CSecureStr
  11. //
  12. #include "PPdefs.h"
  13. #include "passport.h"
  14. typedef int INTERNET_SCHEME;
  15. #include "session.h"
  16. #include "ole2.h"
  17. #include "logon.h"
  18. #include "wincrypt.h"
  19. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  20. #define RTL_ENCRYPT_MEMORY_SIZE 8
  21. typedef NTSTATUS (WINAPI * ENCRYPTIONFUNCTION)(PVOID, ULONG, ULONG);
  22. NTSTATUS
  23. (WINAPI * _I_EncryptMemory)(
  24. IN OUT PVOID Memory,
  25. IN ULONG MemoryLength,
  26. IN ULONG OptionFlags
  27. );
  28. NTSTATUS
  29. (WINAPI * _I_DecryptMemory)(
  30. IN OUT PVOID Memory,
  31. IN ULONG MemoryLength,
  32. IN ULONG OptionFlags
  33. );
  34. HMODULE hAdvApi32Dll;
  35. //
  36. // methods
  37. //
  38. BOOL LoadEncryptionFunctions()
  39. {
  40. if (NULL == hAdvApi32Dll)
  41. {
  42. hAdvApi32Dll = LoadLibrary("ADVAPI32.DLL");
  43. if (hAdvApi32Dll)
  44. {
  45. _I_EncryptMemory = (ENCRYPTIONFUNCTION) GetProcAddress(hAdvApi32Dll, "SystemFunction040");
  46. _I_DecryptMemory = (ENCRYPTIONFUNCTION) GetProcAddress(hAdvApi32Dll, "SystemFunction041");
  47. }
  48. }
  49. return (_I_EncryptMemory != NULL && _I_DecryptMemory != NULL);
  50. }
  51. LPWSTR CSecureStr::GetUnencryptedString()
  52. {
  53. if (NULL == _lpsz)
  54. return NULL;
  55. LPWSTR lpszUnencryptedString = new WCHAR[_stringLength];
  56. if (lpszUnencryptedString != NULL)
  57. {
  58. memcpy(lpszUnencryptedString, _lpsz, _stringLength * sizeof(WCHAR));
  59. if (_fEncryptString)
  60. {
  61. _I_DecryptMemory(lpszUnencryptedString, _stringLength * sizeof(WCHAR), 0);
  62. }
  63. }
  64. return lpszUnencryptedString;
  65. }
  66. BOOL CSecureStr::SetData(LPCWSTR lpszIn)
  67. {
  68. PP_ASSERT(lpszIn != NULL);
  69. DWORD dwStrLen = (wcslen(lpszIn) + 1) * sizeof(WCHAR);
  70. if (_fEncryptString && LoadEncryptionFunctions())
  71. {
  72. DWORD dwLen = 0;
  73. LPWSTR lpszTemp;
  74. dwLen = dwStrLen + (RTL_ENCRYPT_MEMORY_SIZE - dwStrLen % RTL_ENCRYPT_MEMORY_SIZE);
  75. lpszTemp = (LPWSTR) new CHAR[dwLen]; // dwLen is bytes not wide chars
  76. if (!lpszTemp)
  77. return FALSE;
  78. ZeroMemory(lpszTemp, dwLen);
  79. memcpy(lpszTemp, lpszIn, dwStrLen);
  80. NTSTATUS status = _I_EncryptMemory(lpszTemp, dwLen, 0);
  81. if (! NT_SUCCESS(status))
  82. {
  83. _fEncryptString = FALSE;
  84. memcpy(lpszTemp, lpszIn, dwStrLen);
  85. dwLen = dwStrLen;
  86. }
  87. Free(); // release current buffer if it exists
  88. _lpsz = lpszTemp;
  89. PP_ASSERT((dwLen % 2) == 0);
  90. _stringLength = dwLen / sizeof(WCHAR);
  91. return TRUE;
  92. }
  93. else
  94. {
  95. // Make a copy of the data passed in.
  96. LPWSTR lpszTemp = new WCHAR[wcslen(lpszIn) + 1];
  97. if (!lpszTemp)
  98. return FALSE;
  99. Free(); // release current buffer if it exists
  100. memcpy(lpszTemp, lpszIn, dwStrLen);
  101. _lpsz = lpszTemp;
  102. _stringLength = wcslen(lpszIn) + 1;
  103. _fEncryptString = FALSE;
  104. return TRUE;
  105. }
  106. }