[System.Security.SecurityCritical] // auto-generated private void ProtectMemory(SafeBSTRHandle decryptedBuffer) { Debug.Assert(!decryptedBuffer.IsInvalid, "Invalid buffer!"); if (_decryptedLength == 0) { return; } try { SafeBSTRHandle newEncryptedBuffer = null; if (Interop.Crypt32.CryptProtectData(decryptedBuffer, out newEncryptedBuffer)) { _encryptedBuffer.Dispose(); _encryptedBuffer = newEncryptedBuffer; } else { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { decryptedBuffer.ClearBuffer(); } }
[System.Security.SecuritySafeCritical] // auto-generated private unsafe void InsertAtCore(int index, char c) { byte * bufferPtr = null; SafeBSTRHandle decryptedBuffer = null; try { decryptedBuffer = UnProtectMemory(); EnsureCapacity(ref decryptedBuffer, _decryptedLength + 1); decryptedBuffer.AcquirePointer(ref bufferPtr); char *pBuffer = (char *)bufferPtr; for (int i = _decryptedLength; i > index; i--) { pBuffer[i] = pBuffer[i - 1]; } pBuffer[index] = c; ++_decryptedLength; } finally { ProtectMemory(decryptedBuffer); if (bufferPtr != null) { decryptedBuffer.ReleasePointer(); } } }
internal static SafeBSTRHandle Allocate(String src, uint len) { SafeBSTRHandle bstr = SysAllocStringLen(src, len); bstr.Initialize(len * sizeof(char)); return(bstr); }
internal unsafe static void Copy(SafeBSTRHandle source, SafeBSTRHandle target, uint bytesToCopy) { if (bytesToCopy == 0) { return; } byte* sourcePtr = null, targetPtr = null; try { source.AcquirePointer(ref sourcePtr); target.AcquirePointer(ref targetPtr); Debug.Assert(source.ByteLength >= bytesToCopy, "Source buffer is too small."); Buffer.MemoryCopy(sourcePtr, targetPtr, target.ByteLength, bytesToCopy); } finally { if (targetPtr != null) { target.ReleasePointer(); } if (sourcePtr != null) { source.ReleasePointer(); } } }
internal unsafe static void Copy(SafeBSTRHandle source, SafeBSTRHandle target) { byte *sourcePtr = null, targetPtr = null; RuntimeHelpers.PrepareConstrainedRegions(); try { source.AcquirePointer(ref sourcePtr); target.AcquirePointer(ref targetPtr); Contract.Assert(Win32Native.SysStringLen((IntPtr)targetPtr) >= Win32Native.SysStringLen((IntPtr)sourcePtr), "Target buffer is not large enough!"); Buffer.Memcpy(targetPtr, sourcePtr, (int)Win32Native.SysStringLen((IntPtr)sourcePtr) * 2); } finally { if (sourcePtr != null) { source.ReleasePointer(); } if (targetPtr != null) { target.ReleasePointer(); } } }
[System.Security.SecuritySafeCritical] // auto-generated private unsafe void RemoveAtCore(int index) { byte * bufferPtr = null; SafeBSTRHandle decryptedBuffer = null; try { decryptedBuffer = UnProtectMemory(); decryptedBuffer.AcquirePointer(ref bufferPtr); char *pBuffer = (char *)bufferPtr; for (int i = index; i < _decryptedLength - 1; i++) { pBuffer[i] = pBuffer[i + 1]; } pBuffer[--_decryptedLength] = (char)0; } finally { ProtectMemory(decryptedBuffer); if (bufferPtr != null) { decryptedBuffer.ReleasePointer(); } } }
[System.Security.SecuritySafeCritical] // auto-generated public void SetAt(int index, char c) { lock (_methodLock) { if (index < 0 || index >= _decryptedLength) { throw new ArgumentOutOfRangeException("index", SR.ArgumentOutOfRange_IndexString); } Debug.Assert(index <= Int32.MaxValue / sizeof(char)); EnsureNotDisposed(); EnsureNotReadOnly(); SafeBSTRHandle decryptedBuffer = null; try { decryptedBuffer = UnProtectMemory(); decryptedBuffer.Write <char>((uint)index * sizeof(char), c); } finally { ProtectMemory(decryptedBuffer); } } }
[System.Security.SecurityCritical] // auto-generated internal SecureString(SecureString str) { AllocateBuffer(str.BufferLength); SafeBSTRHandle.Copy(str.m_buffer, this.m_buffer); m_length = str.m_length; m_encrypted = str.m_encrypted; }
internal static SafeBSTRHandle Allocate(string src, uint len) { SafeBSTRHandle handle = SysAllocStringLen(src, len); handle.Initialize((ulong)(len * 2)); return(handle); }
[System.Security.SecurityCritical] // auto-generated private unsafe void InitializeSecureString(char *value, int length) { if (length == 0) { AllocateBuffer(0); _decryptedLength = 0; return; } _encryptedBuffer = SafeBSTRHandle.Allocate(null, 0); SafeBSTRHandle decryptedBuffer = SafeBSTRHandle.Allocate(null, (uint)length); _decryptedLength = length; byte *bufferPtr = null; try { decryptedBuffer.AcquirePointer(ref bufferPtr); Buffer.MemoryCopy((byte *)value, bufferPtr, decryptedBuffer.Length * sizeof(char), length * sizeof(char)); } finally { if (bufferPtr != null) { decryptedBuffer.ReleasePointer(); } } ProtectMemory(decryptedBuffer); }
internal static unsafe bool CryptProtectData(SafeBSTRHandle uncryptedBuffer, out SafeBSTRHandle cryptedBuffer) { byte* uncryptedBufferPtr = null; DATA_BLOB pDataOut = default(DATA_BLOB); try { uncryptedBuffer.AcquirePointer(ref uncryptedBufferPtr); DATA_BLOB pDataIn = new DATA_BLOB((IntPtr)uncryptedBufferPtr, uncryptedBuffer.Length * 2); if (CryptProtectData(new IntPtr(&pDataIn), String.Empty, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, CRYPTPROTECTMEMORY_SAME_PROCESS, new IntPtr(&pDataOut))) { SafeBSTRHandle newHandle = SafeBSTRHandle.Allocate(pDataOut.pbData, pDataOut.cbData); cryptedBuffer = newHandle; return true; } else { cryptedBuffer = SafeBSTRHandle.Allocate(null, 0); return false; } } finally { if (uncryptedBufferPtr != null) uncryptedBuffer.ReleasePointer(); if (pDataOut.pbData != IntPtr.Zero) { NtDll.ZeroMemory(pDataOut.pbData, (UIntPtr)pDataOut.cbData); Marshal.FreeHGlobal(pDataOut.pbData); } } }
internal unsafe static void Copy(SafeBSTRHandle source, SafeBSTRHandle target, uint bytesToCopy) { if (bytesToCopy == 0) { return; } byte *sourcePtr = null, targetPtr = null; try { source.AcquirePointer(ref sourcePtr); target.AcquirePointer(ref targetPtr); Debug.Assert(Interop.OleAut32.SysStringLen((IntPtr)sourcePtr) * sizeof(char) >= bytesToCopy, "Source buffer is too small."); Buffer.MemoryCopy(sourcePtr, targetPtr, Interop.OleAut32.SysStringLen((IntPtr)targetPtr) * sizeof(char), bytesToCopy); } finally { if (sourcePtr != null) { source.ReleasePointer(); } if (targetPtr != null) { target.ReleasePointer(); } } }
internal static unsafe void Copy(SafeBSTRHandle source, SafeBSTRHandle target, uint bytesToCopy) { if (bytesToCopy == 0) { return; } byte *sourcePtr = null, targetPtr = null; try { source.AcquirePointer(ref sourcePtr); target.AcquirePointer(ref targetPtr); Debug.Assert(source.ByteLength >= bytesToCopy, "Source buffer is too small."); Buffer.MemoryCopy(sourcePtr, targetPtr, target.ByteLength, bytesToCopy); } finally { if (targetPtr != null) { target.ReleasePointer(); } if (sourcePtr != null) { source.ReleasePointer(); } } }
internal static SafeBSTRHandle Allocate(IntPtr src, uint lenInBytes) { SafeBSTRHandle bstr = Interop.OleAut32.SysAllocStringLen(src, lenInBytes / sizeof(char)); bstr.Initialize(lenInBytes); return(bstr); }
private void AllocateBuffer(int size) { uint alignedSize = GetAlignedSize(size); m_buffer = SafeBSTRHandle.Allocate(null, alignedSize); if (m_buffer.IsInvalid) { throw new OutOfMemoryException(); } }
internal static SafeBSTRHandle Allocate(string src, uint len) { SafeBSTRHandle safeBstrHandle = SafeBSTRHandle.SysAllocStringLen(src, len); long num = (long)(len * 2U); safeBstrHandle.Initialize((ulong)num); return(safeBstrHandle); }
public void Dispose() { if ((this.m_buffer != null) && !this.m_buffer.IsInvalid) { this.m_buffer.Close(); this.m_buffer = null; } }
private void AllocateBuffer(int size) { this.m_buffer = SafeBSTRHandle.Allocate((string)null, SecureString.GetAlignedSize(size)); if (this.m_buffer.IsInvalid) { throw new OutOfMemoryException(); } }
[System.Security.SecurityCritical] // auto-generated private void AllocateBuffer(uint size) { _encryptedBuffer = SafeBSTRHandle.Allocate(null, size); if (_encryptedBuffer.IsInvalid) { throw new OutOfMemoryException(); } }
public void Dispose() { if (m_buffer != null && !m_buffer.IsInvalid) { m_buffer.Close(); m_buffer = null; } }
public void Dispose() { if (this.m_buffer == null || this.m_buffer.IsInvalid) { return; } this.m_buffer.Close(); this.m_buffer = (SafeBSTRHandle)null; }
private void AllocateBuffer(int size) { uint alignedSize = GetAlignedSize(size); this.m_buffer = SafeBSTRHandle.Allocate(null, alignedSize); if (this.m_buffer.IsInvalid) { throw new OutOfMemoryException(); } }
[System.Security.SecuritySafeCritical] // auto-generated public void Dispose() { lock (_methodLock) { if (_encryptedBuffer != null && !_encryptedBuffer.IsInvalid) { _encryptedBuffer.Dispose(); _encryptedBuffer = null; } } }
internal static SafeBSTRHandle Allocate(string src, uint lenInChars) { SafeBSTRHandle bstr = Interop.OleAut32.SysAllocStringLen(src, lenInChars); if (bstr.IsInvalid) // SysAllocStringLen returns a NULL ptr when there's insufficient memory { throw new OutOfMemoryException(); } bstr.Initialize(lenInChars * sizeof(char)); return(bstr); }
internal static SafeBSTRHandle Allocate(IntPtr src, uint lenInBytes) { Debug.Assert(lenInBytes % sizeof(char) == 0); SafeBSTRHandle bstr = Interop.OleAut32.SysAllocStringLen(src, lenInBytes / sizeof(char)); if (bstr.IsInvalid) // SysAllocStringLen returns a NULL ptr when there's insufficient memory { throw new OutOfMemoryException(); } bstr.Initialize(lenInBytes); return(bstr); }
internal SecureString(SecureString str) { Debug.Assert(str != null, "Expected non-null SecureString"); Debug.Assert(str._buffer != null, "Expected other SecureString's buffer to be non-null"); Debug.Assert(str._encrypted, "Expected to be used only on encrypted SecureStrings"); AllocateBuffer(str._buffer.Length); SafeBSTRHandle.Copy(str._buffer, _buffer, str._buffer.Length * sizeof(char)); _decryptedLength = str._decryptedLength; _encrypted = str._encrypted; }
private static bool EncryptionSupported() { bool flag = true; try { Win32Native.SystemFunction041(SafeBSTRHandle.Allocate(null, 0x10), 0x10, 0); } catch (EntryPointNotFoundException) { flag = false; } return(flag); }
private static bool EncryptionSupported() { bool flag = true; try { Win32Native.SystemFunction041(SafeBSTRHandle.Allocate((string)null, 16U), 16U, 0U); } catch (EntryPointNotFoundException ex) { flag = false; } return(flag); }
[System.Security.SecurityCritical] // auto-generated unsafe static bool EncryptionSupported() { // check if the enrypt/decrypt function is supported on current OS bool supported = true; try { Win32Native.SystemFunction041( SafeBSTRHandle.Allocate(null , (int)Win32Native.CRYPTPROTECTMEMORY_BLOCK_SIZE), Win32Native.CRYPTPROTECTMEMORY_BLOCK_SIZE, Win32Native.CRYPTPROTECTMEMORY_SAME_PROCESS); } catch (EntryPointNotFoundException) { supported = false; } return supported; }
[System.Security.SecuritySafeCritical] // auto-generated private void SetAtCore(int index, char c) { SafeBSTRHandle decryptedBuffer = null; try { decryptedBuffer = UnProtectMemory(); decryptedBuffer.Write <char>((uint)index * sizeof(char), c); } finally { ProtectMemory(decryptedBuffer); } }
private static bool EncryptionSupported() { bool result = true; try { Win32Native.SystemFunction041(SafeBSTRHandle.Allocate(null, 16U), 16U, 0U); } catch (EntryPointNotFoundException) { result = false; } return(result); }
[System.Security.SecuritySafeCritical] // auto-generated private void AppendCharCore(char c) { SafeBSTRHandle decryptedBuffer = null; try { decryptedBuffer = UnProtectMemory(); EnsureCapacity(ref decryptedBuffer, _decryptedLength + 1); decryptedBuffer.Write <char>((uint)_decryptedLength * sizeof(char), c); _decryptedLength++; } finally { ProtectMemory(decryptedBuffer); } }
[System.Security.SecurityCritical] // auto-generated internal unsafe IntPtr ToUniStr() { lock (_methodLock) { EnsureNotDisposed(); int length = _decryptedLength; IntPtr ptr = IntPtr.Zero; IntPtr result = IntPtr.Zero; byte * bufferPtr = null; SafeBSTRHandle decryptedBuffer = null; try { ptr = Marshal.AllocCoTaskMem((length + 1) * 2); if (ptr == IntPtr.Zero) { throw new OutOfMemoryException(); } decryptedBuffer = UnProtectMemory(); decryptedBuffer.AcquirePointer(ref bufferPtr); Buffer.MemoryCopy(bufferPtr, (byte *)ptr.ToPointer(), ((length + 1) * 2), length * 2); char *endptr = (char *)ptr.ToPointer(); *(endptr + length) = '\0'; result = ptr; } finally { if (result == IntPtr.Zero) { // If we failed for any reason, free the new buffer if (ptr != IntPtr.Zero) { Interop.NtDll.ZeroMemory(ptr, (UIntPtr)(length * 2)); Marshal.FreeCoTaskMem(ptr); } } if (bufferPtr != null) { decryptedBuffer.ReleasePointer(); } } return(result); } }
internal unsafe static void Copy(SafeBSTRHandle source, SafeBSTRHandle target, uint bytesToCopy) { if (bytesToCopy == 0) return; byte* sourcePtr = null, targetPtr = null; try { source.AcquirePointer(ref sourcePtr); target.AcquirePointer(ref targetPtr); Debug.Assert(Interop.OleAut32.SysStringLen((IntPtr)sourcePtr) * sizeof(char) >= bytesToCopy, "Source buffer is too small."); Buffer.MemoryCopy(sourcePtr, targetPtr, Interop.OleAut32.SysStringLen((IntPtr)targetPtr) * sizeof(char), bytesToCopy); } finally { if (sourcePtr != null) source.ReleasePointer(); if (targetPtr != null) target.ReleasePointer(); } }
internal static unsafe void Copy(SafeBSTRHandle source, SafeBSTRHandle target) { byte* pointer = null; byte* numPtr2 = null; RuntimeHelpers.PrepareConstrainedRegions(); try { source.AcquirePointer(ref pointer); target.AcquirePointer(ref numPtr2); Buffer.memcpyimpl(pointer, numPtr2, Win32Native.SysStringLen((IntPtr) pointer) * 2); } finally { if (pointer != null) { source.ReleasePointer(); } if (numPtr2 != null) { target.ReleasePointer(); } } }
private void EnsureCapacity(int capacity) { if (capacity > MaxLength) { throw new ArgumentOutOfRangeException(nameof(capacity), SR.ArgumentOutOfRange_Capacity); } if (((uint)capacity * sizeof(char)) <= _buffer.ByteLength) { return; } var oldBuffer = _buffer; SafeBSTRHandle newBuffer = SafeBSTRHandle.Allocate(GetAlignedSize((uint)capacity)); SafeBSTRHandle.Copy(oldBuffer, newBuffer, (uint)_decryptedLength * sizeof(char)); _buffer = newBuffer; oldBuffer.Dispose(); }
internal unsafe static void Copy(SafeBSTRHandle source, SafeBSTRHandle target) { byte* sourcePtr = null, targetPtr = null; RuntimeHelpers.PrepareConstrainedRegions(); try { source.AcquirePointer(ref sourcePtr); target.AcquirePointer(ref targetPtr); Contract.Assert(Win32Native.SysStringLen((IntPtr)targetPtr) >= Win32Native.SysStringLen((IntPtr)sourcePtr), "Target buffer is not large enough!"); Buffer.Memcpy(targetPtr, sourcePtr, (int) Win32Native.SysStringLen((IntPtr)sourcePtr) * 2); } finally { if (sourcePtr != null) source.ReleasePointer(); if (targetPtr != null) target.ReleasePointer(); } }
internal static extern bool CryptUnprotectMemory(SafeBSTRHandle pData, uint cbData, uint dwFlags);
[System.Security.SecurityCritical] // auto-generated private void AllocateBuffer(uint size) { _encryptedBuffer = SafeBSTRHandle.Allocate(null, size); }
internal static extern uint SysStringLen(SafeBSTRHandle bstr);
[System.Security.SecurityCritical] // auto-generated private void EnsureCapacity(ref SafeBSTRHandle decryptedBuffer, int capacity) { if (capacity > MaxLength) { throw new ArgumentOutOfRangeException("capacity", SR.ArgumentOutOfRange_Capacity); } if (capacity <= _decryptedLength) { return; } SafeBSTRHandle newBuffer = SafeBSTRHandle.Allocate(null, (uint)capacity); SafeBSTRHandle.Copy(decryptedBuffer, newBuffer, (uint)_decryptedLength * sizeof(char)); decryptedBuffer.Dispose(); decryptedBuffer = newBuffer; }
[System.Security.SecurityCritical] // auto-generated private void ProtectMemory(SafeBSTRHandle decryptedBuffer) { Debug.Assert(!decryptedBuffer.IsInvalid, "Invalid buffer!"); if (_decryptedLength == 0) { return; } try { SafeBSTRHandle newEncryptedBuffer = null; if (Interop.Crypt32.CryptProtectData(decryptedBuffer, out newEncryptedBuffer)) { _encryptedBuffer.Dispose(); _encryptedBuffer = newEncryptedBuffer; } else { throw new CryptographicException(Marshal.GetLastWin32Error()); } } finally { decryptedBuffer.ClearBuffer(); decryptedBuffer.Dispose(); } }
private void AllocateBuffer(uint size) { _buffer = SafeBSTRHandle.Allocate(GetAlignedSize(size)); }
internal static extern void ZeroMemory(SafeBSTRHandle address, uint length);
public void Dispose() { if(m_buffer != null && !m_buffer.IsInvalid) { m_buffer.Close(); m_buffer = null; } }
private void EnsureCapacity(int capacity) { if (capacity > 0x10000) { throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_Capacity")); } if (capacity > this.m_buffer.Length) { SafeBSTRHandle target = SafeBSTRHandle.Allocate(null, GetAlignedSize(capacity)); if (target.IsInvalid) { throw new OutOfMemoryException(); } SafeBSTRHandle.Copy(this.m_buffer, target); this.m_buffer.Close(); this.m_buffer = target; } }
[System.Security.SecurityCritical] // auto-generated private void EnsureCapacity(int capacity) { if( capacity > MaxLength) { throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_Capacity")); } Contract.EndContractBlock(); if( capacity <= m_buffer.Length) { return; } SafeBSTRHandle newBuffer = SafeBSTRHandle.Allocate(null, GetAlignedSize(capacity)); if (newBuffer.IsInvalid) { throw new OutOfMemoryException(); } SafeBSTRHandle.Copy(m_buffer, newBuffer); m_buffer.Close(); m_buffer = newBuffer; }