using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
+using Microsoft.Win32;
namespace System.Security
{
}
}
+ internal unsafe IntPtr MarshalToBSTR()
+ {
+ int length = _decryptedLength;
+ IntPtr ptr = IntPtr.Zero;
+ IntPtr result = IntPtr.Zero;
+ byte* bufferPtr = null;
+
+ UnprotectMemory();
+ try
+ {
+ _buffer.AcquirePointer(ref bufferPtr);
+ int resultByteLength = (length + 1) * sizeof(char);
+
+ ptr = Win32Native.SysAllocStringLen(null, length);
+ if (ptr == IntPtr.Zero) {
+ throw new OutOfMemoryException();
+ }
+
+ Buffer.MemoryCopy(bufferPtr, (byte*)ptr, resultByteLength, length * sizeof(char));
+
+ result = ptr;
+ }
+ finally
+ {
+ ProtectMemory();
+
+ // If we failed for any reason, free the new buffer
+ if (result == IntPtr.Zero && ptr != IntPtr.Zero)
+ {
+ Interop.NtDll.ZeroMemory(ptr, (UIntPtr)(length * sizeof(char)));
+ Win32Native.SysFreeString(ptr);
+ }
+
+ if (bufferPtr != null)
+ {
+ _buffer.ReleasePointer();
+ }
+ }
+ return result;
+ }
+
internal unsafe IntPtr MarshalToStringCore(bool globalAlloc, bool unicode)
{
int length = _decryptedLength;
<Member Name="ZeroFreeGlobalAllocAnsi(System.IntPtr)" />
<Member Name="ZeroFreeGlobalAllocUnicode(System.IntPtr)" />
<Member Name="ZeroFreeCoTaskMemUTF8(System.IntPtr)" />
+ <Member Name="SecureStringToCoTaskMemAnsi(System.Security.SecureString)" />
+ <Member Name="SecureStringToCoTaskMemUnicode(System.Security.SecureString)" />
+ <Member Name="SecureStringToGlobalAllocAnsi(System.Security.SecureString)" />
+ <Member Name="SecureStringToGlobalAllocUnicode(System.Security.SecureString)" />
+ <Member Name="SecureStringToBSTR(System.Security.SecureString)" />
</Type>
<Type Name="System.Runtime.InteropServices.MarshalAsAttribute">
<Member MemberType="Field" Name="ArraySubType" />
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern IntPtr GetFunctionPointerForDelegateInternal(Delegate d);
-#if FEATURE_LEGACYSURFACE
-
-#if FEATURE_COMINTEROP
[System.Security.SecurityCritical] // auto-generated_required
public static IntPtr SecureStringToBSTR(SecureString s) {
if( s == null) {
}
Contract.EndContractBlock();
- return s.ToBSTR();
- }
+#if FEATURE_COMINTEROP
+ return s.MarshalToBSTR();
+#else
+ throw new PlatformNotSupportedException();
#endif
+ }
[System.Security.SecurityCritical] // auto-generated_required
public static IntPtr SecureStringToCoTaskMemAnsi(SecureString s) {
}
Contract.EndContractBlock();
- return s.ToAnsiStr(false);
+ return s.MarshalToString(globalAlloc: false, unicode: false);
}
[System.Security.SecurityCritical] // auto-generated_required
public static IntPtr SecureStringToCoTaskMemUnicode(SecureString s)
{
- if (s == null)
- {
+ if( s == null) {
throw new ArgumentNullException(nameof(s));
}
Contract.EndContractBlock();
- return s.ToUniStr(false);
+ return s.MarshalToString(globalAlloc: false, unicode: true);
}
-#endif // FEATURE_LEGACYSURFACE
-
-
#if FEATURE_COMINTEROP
[System.Security.SecurityCritical] // auto-generated_required
public static void ZeroFreeBSTR(IntPtr s)
FreeCoTaskMem(s);
}
-#if FEATURE_LEGACYSURFACE
[System.Security.SecurityCritical] // auto-generated_required
public static IntPtr SecureStringToGlobalAllocAnsi(SecureString s) {
if( s == null) {
}
Contract.EndContractBlock();
- return s.ToAnsiStr(true);
+ return s.MarshalToString(globalAlloc: true, unicode: false);
}
[System.Security.SecurityCritical] // auto-generated_required
}
Contract.EndContractBlock();
- return s.ToUniStr(true);
+ return s.MarshalToString(globalAlloc: true, unicode: true);;
}
-#endif // FEATURE_LEGACYSURFACE
[System.Security.SecurityCritical] // auto-generated_required
public static void ZeroFreeGlobalAllocAnsi(IntPtr s) {