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.

168 lines
3.7 KiB

  1. /*++
  2. Copyright (c) 2003 Microsoft Corporation
  3. Module Name:
  4. CSecureStr.h
  5. Abstract:
  6. Author:
  7. Stephen A Sulzer (ssulzer) 16-Jan-2003
  8. --*/
  9. //
  10. // class implementation of CSecureStr
  11. //
  12. #include "include.hxx"
  13. typedef LONG NTSTATUS;
  14. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  15. typedef NTSTATUS (WINAPI * ENCRYPTIONFUNCTION)(PVOID, ULONG, ULONG);
  16. NTSTATUS
  17. (WINAPI * _I_EncryptMemory)(
  18. IN OUT PVOID Memory,
  19. IN ULONG MemoryLength,
  20. IN ULONG OptionFlags
  21. );
  22. NTSTATUS
  23. (WINAPI * _I_DecryptMemory)(
  24. IN OUT PVOID Memory,
  25. IN ULONG MemoryLength,
  26. IN ULONG OptionFlags
  27. );
  28. HMODULE hAdvApi32Dll;
  29. // From <crypt.h> .....
  30. //
  31. // The buffer passed into RtlEncryptMemory and RtlDecryptMemory
  32. // must be a multiple of this length.
  33. //
  34. #define RTL_ENCRYPT_MEMORY_SIZE 8
  35. //
  36. // Allow Encrypt/Decrypt across process boundaries.
  37. // eg: encrypted buffer passed across LPC to another process which calls RtlDecryptMemory.
  38. //
  39. #define RTL_ENCRYPT_OPTION_CROSS_PROCESS 0x01
  40. //
  41. // Allow Encrypt/Decrypt across callers with same LogonId.
  42. // eg: encrypted buffer passed across LPC to another process which calls RtlDecryptMemory whilst impersonating.
  43. //
  44. #define RTL_ENCRYPT_OPTION_SAME_LOGON 0x02
  45. //
  46. // methods
  47. //
  48. BOOL LoadEncryptionFunctions()
  49. {
  50. if (NULL == hAdvApi32Dll)
  51. {
  52. hAdvApi32Dll = LoadLibrary("ADVAPI32.DLL");
  53. if (hAdvApi32Dll)
  54. {
  55. _I_EncryptMemory = (ENCRYPTIONFUNCTION) GetProcAddress(hAdvApi32Dll, "SystemFunction040");
  56. _I_DecryptMemory = (ENCRYPTIONFUNCTION) GetProcAddress(hAdvApi32Dll, "SystemFunction041");
  57. }
  58. }
  59. return (_I_EncryptMemory != NULL && _I_DecryptMemory != NULL);
  60. }
  61. LPSTR CSecureStr::GetUnencryptedString()
  62. {
  63. if (NULL == _lpsz)
  64. return NULL;
  65. LPSTR lpszUnencryptedString = new CHAR[_stringLength];
  66. if (lpszUnencryptedString != NULL)
  67. {
  68. memcpy(lpszUnencryptedString, _lpsz, _stringLength);
  69. if (_fEncryptString && LoadEncryptionFunctions())
  70. {
  71. _I_DecryptMemory(lpszUnencryptedString, _stringLength, RTL_ENCRYPT_OPTION_SAME_LOGON);
  72. }
  73. }
  74. return lpszUnencryptedString;
  75. }
  76. BOOL CSecureStr::SetData(LPSTR lpszIn)
  77. {
  78. DIGEST_ASSERT(lpszIn != NULL);
  79. if (_fEncryptString && LoadEncryptionFunctions())
  80. {
  81. DWORD dwStrLen = strlen(lpszIn) + 1;
  82. DWORD dwLen = 0;
  83. LPSTR lpszTemp;
  84. dwLen = dwStrLen + (RTL_ENCRYPT_MEMORY_SIZE - dwStrLen % RTL_ENCRYPT_MEMORY_SIZE);
  85. DIGEST_ASSERT((dwLen % 8) == 0);
  86. lpszTemp = new CHAR[dwLen + 1];
  87. if (!lpszTemp)
  88. return FALSE;
  89. ZeroMemory(lpszTemp, dwLen);
  90. memcpy(lpszTemp, lpszIn, dwStrLen);
  91. NTSTATUS status = _I_EncryptMemory(lpszTemp, dwLen, RTL_ENCRYPT_OPTION_SAME_LOGON);
  92. if (! NT_SUCCESS(status))
  93. {
  94. _fEncryptString = FALSE;
  95. memcpy(lpszTemp, lpszIn, dwStrLen);
  96. dwLen = dwStrLen;
  97. }
  98. Free(); // release current buffer if it exists
  99. _lpsz = lpszTemp;
  100. _stringLength = dwLen;
  101. _fOwnString = true;
  102. return TRUE;
  103. }
  104. else
  105. {
  106. // Make a copy of the data passed in.
  107. LPSTR lpszTemp = NewString(lpszIn);
  108. if (!lpszTemp)
  109. return FALSE;
  110. Free(); // release current buffer if it exists
  111. _lpsz = lpszTemp;
  112. _stringLength = strlen(_lpsz) + 1;
  113. _fEncryptString = false;
  114. _fOwnString = true;
  115. return TRUE;
  116. }
  117. }