14 private SafeBSTRHandle m_buffer;
18 private bool m_readOnly;
20 private bool m_encrypted;
22 private static bool supportedOnCurrentPlatform;
24 private const int BlockSize = 8;
26 private const int MaxLength = 65536;
28 private const uint ProtectionScope = 0u;
36 [SecuritySafeCritical]
44 private int BufferLength
49 return m_buffer.Length;
53 [SecuritySafeCritical]
56 supportedOnCurrentPlatform = EncryptionSupported();
60 private static bool EncryptionSupported()
65 Win32Native.SystemFunction041(SafeBSTRHandle.Allocate(
null, 16u), 16u, 0u);
68 catch (EntryPointNotFoundException)
75 internal SecureString(SecureString str)
77 AllocateBuffer(str.BufferLength);
78 SafeBSTRHandle.Copy(str.m_buffer, m_buffer);
79 m_length = str.m_length;
80 m_encrypted = str.m_encrypted;
86 [SecuritySafeCritical]
89 CheckSupportedOnCurrentPlatform();
95 [HandleProcessCorruptedStateExceptions]
96 private unsafe
void InitializeSecureString(
char* value,
int length)
98 CheckSupportedOnCurrentPlatform();
99 AllocateBuffer(length);
101 byte* pointer =
null;
105 m_buffer.AcquirePointer(ref pointer);
106 Buffer.Memcpy(pointer, (
byte*)value, length * 2);
117 m_buffer.ReleasePointer();
133 [CLSCompliant(
false)]
148 InitializeSecureString(value, length);
158 [SecuritySafeCritical]
159 [HandleProcessCorruptedStateExceptions]
164 EnsureCapacity(m_length + 1);
169 m_buffer.Write((uint)(m_length * 2), c);
187 [SecuritySafeCritical]
193 m_buffer.ClearBuffer();
202 [SecuritySafeCritical]
211 [SecuritySafeCritical]
214 if (m_buffer !=
null && !m_buffer.IsInvalid)
230 [SecuritySafeCritical]
231 [HandleProcessCorruptedStateExceptions]
234 if (index < 0 || index > m_length)
240 EnsureCapacity(m_length + 1);
241 byte* pointer =
null;
246 m_buffer.AcquirePointer(ref pointer);
247 char* ptr = (
char*)pointer;
248 for (
int num = m_length; num > index; num--)
250 ptr[num] = ptr[num - 1];
265 m_buffer.ReleasePointer();
275 [SecuritySafeCritical]
285 [SecuritySafeCritical]
300 [SecuritySafeCritical]
301 [HandleProcessCorruptedStateExceptions]
306 if (index < 0 || index >= m_length)
310 byte* pointer =
null;
315 m_buffer.AcquirePointer(ref pointer);
316 char* ptr = (
char*)pointer;
317 for (
int i = index; i < m_length - 1; i++)
321 ptr[--m_length] =
'\0';
333 m_buffer.ReleasePointer();
347 [SecuritySafeCritical]
348 [HandleProcessCorruptedStateExceptions]
349 public void SetAt(
int index,
char c)
351 if (index < 0 || index >= m_length)
361 m_buffer.Write((uint)(index * 2), c);
375 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
376 private void AllocateBuffer(
int size)
378 uint alignedSize = GetAlignedSize(size);
379 m_buffer = SafeBSTRHandle.Allocate(
null, alignedSize);
380 if (m_buffer.IsInvalid)
386 private void CheckSupportedOnCurrentPlatform()
388 if (!supportedOnCurrentPlatform)
390 throw new NotSupportedException(Environment.GetResourceString(
"Arg_PlatformSecureString"));
395 private void EnsureCapacity(
int capacity)
397 if (capacity > 65536)
399 throw new ArgumentOutOfRangeException(
"capacity", Environment.GetResourceString(
"ArgumentOutOfRange_Capacity"));
401 if (capacity > m_buffer.Length)
403 SafeBSTRHandle safeBSTRHandle = SafeBSTRHandle.Allocate(
null, GetAlignedSize(capacity));
404 if (safeBSTRHandle.IsInvalid)
406 throw new OutOfMemoryException();
408 SafeBSTRHandle.Copy(m_buffer, safeBSTRHandle);
410 m_buffer = safeBSTRHandle;
415 private void EnsureNotDisposed()
417 if (m_buffer ==
null)
419 throw new ObjectDisposedException(
null);
423 private void EnsureNotReadOnly()
427 throw new InvalidOperationException(Environment.GetResourceString(
"InvalidOperation_ReadOnly"));
431 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
432 private static uint GetAlignedSize(
int size)
434 uint num = (uint)size / 8u * 8;
435 if (size % 8 != 0 || size == 0)
443 private unsafe
int GetAnsiByteCount()
447 byte* pointer =
null;
451 m_buffer.AcquirePointer(ref pointer);
452 return Win32Native.WideCharToMultiByte(0u, flags, (
char*)pointer, m_length,
null, 0, IntPtr.Zero,
new IntPtr(&num));
458 m_buffer.ReleasePointer();
464 private unsafe
void GetAnsiBytes(
byte* ansiStrPtr,
int byteCount)
468 byte* pointer =
null;
472 m_buffer.AcquirePointer(ref pointer);
473 Win32Native.WideCharToMultiByte(0u, flags, (
char*)pointer, m_length, ansiStrPtr, byteCount - 1, IntPtr.Zero,
new IntPtr(&num));
474 *(ansiStrPtr + byteCount - 1) = 0;
480 m_buffer.ReleasePointer();
486 [ReliabilityContract(
Consistency.MayCorruptInstance,
Cer.MayFail)]
487 private void ProtectMemory()
489 if (m_length != 0 && !m_encrypted)
497 int num = Win32Native.SystemFunction040(m_buffer, (uint)(m_buffer.Length * 2), 0u);
509 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
510 [HandleProcessCorruptedStateExceptions]
511 internal unsafe IntPtr ToBSTR()
514 int length = m_length;
515 IntPtr intPtr = IntPtr.Zero;
516 IntPtr intPtr2 = IntPtr.Zero;
517 byte* pointer =
null;
527 intPtr = Win32Native.SysAllocStringLen(
null, length);
529 if (intPtr == IntPtr.Zero)
531 throw new OutOfMemoryException();
534 m_buffer.AcquirePointer(ref pointer);
535 Buffer.Memcpy((
byte*)intPtr.ToPointer(), pointer, length * 2);
547 if (intPtr2 == IntPtr.Zero && intPtr != IntPtr.Zero)
549 Win32Native.ZeroMemory(intPtr, (UIntPtr)(ulong)(length * 2));
550 Win32Native.SysFreeString(intPtr);
554 m_buffer.ReleasePointer();
561 [HandleProcessCorruptedStateExceptions]
562 internal unsafe IntPtr ToUniStr(
bool allocateFromHeap)
565 int length = m_length;
566 IntPtr intPtr = IntPtr.Zero;
567 IntPtr intPtr2 = IntPtr.Zero;
568 byte* pointer =
null;
580 if (intPtr == IntPtr.Zero)
582 throw new OutOfMemoryException();
585 m_buffer.AcquirePointer(ref pointer);
586 Buffer.Memcpy((
byte*)intPtr.ToPointer(), pointer, length * 2);
587 char* ptr = (
char*)intPtr.ToPointer();
600 if (intPtr2 == IntPtr.Zero && intPtr != IntPtr.Zero)
602 Win32Native.ZeroMemory(intPtr, (UIntPtr)(ulong)(length * 2));
603 if (allocateFromHeap)
614 m_buffer.ReleasePointer();
621 [HandleProcessCorruptedStateExceptions]
622 internal unsafe IntPtr ToAnsiStr(
bool allocateFromHeap)
625 IntPtr intPtr = IntPtr.Zero;
626 IntPtr intPtr2 = IntPtr.Zero;
632 num = GetAnsiByteCount() + 1;
641 if (intPtr == IntPtr.Zero)
643 throw new OutOfMemoryException();
645 GetAnsiBytes((
byte*)intPtr.ToPointer(), num);
657 if (intPtr2 == IntPtr.Zero && intPtr != IntPtr.Zero)
659 Win32Native.ZeroMemory(intPtr, (UIntPtr)(ulong)num);
660 if (allocateFromHeap)
673 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
674 private void UnProtectMemory()
686 int num = Win32Native.SystemFunction041(m_buffer, (uint)(m_buffer.Length * 2), 0u);
The exception that is thrown when an error occurs during a cryptographic operation.
bool IsReadOnly()
Indicates whether this secure string is marked read-only.
unsafe SecureString(char *value, int length)
Initializes a new instance of the T:System.Security.SecureString class from a subarray of T:System....
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void SetAt(int index, char c)
Replaces the existing character at the specified index position with another character.
static void FreeHGlobal(IntPtr hglobal)
Frees memory previously allocated from the unmanaged memory of the process.
static void FreeCoTaskMem(IntPtr ptr)
Frees a block of memory allocated by the unmanaged COM task memory allocator.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
SecureString Copy()
Creates a copy of the current secure string.
Cer
Specifies a method's behavior when called within a constrained execution region.
void AppendChar(char c)
Appends a character to the end of the current secure string.
Provides information about, and means to manipulate, the current environment and platform....
unsafe void InsertAt(int index, char c)
Inserts a character in this secure string at the specified index position.
static IntPtr AllocCoTaskMem(int cb)
Allocates a block of memory of specified size from the COM task memory allocator.
Represents text that should be kept confidential, such as by deleting it from computer memory when no...
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
SecureString()
Initializes a new instance of the T:System.Security.SecureString class.
The exception that is thrown when there is not enough memory to continue the execution of a program.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
MethodImplOptions
Defines the details of how a method is implemented.
void Clear()
Deletes the value of the current secure string.
unsafe void RemoveAt(int index)
Removes the character at the specified index position from this secure string.
int Length
Gets the number of characters in the current secure string.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
void Dispose()
Releases all resources used by the current T:System.Security.SecureString object.
static IntPtr AllocHGlobal(IntPtr cb)
Allocates memory from the unmanaged memory of the process by using the pointer to the specified numbe...
Manipulates arrays of primitive types.
Consistency
Specifies a reliability contract.
void MakeReadOnly()
Makes the text value of this secure string read-only.
Provides a set of static methods and properties that provide support for compilers....