diff options
Diffstat (limited to 'src/mscorlib/shared/System/Security')
6 files changed, 49 insertions, 21 deletions
diff --git a/src/mscorlib/shared/System/Security/CryptographicException.cs b/src/mscorlib/shared/System/Security/CryptographicException.cs index 89cb658aa9..7c4fa176f3 100644 --- a/src/mscorlib/shared/System/Security/CryptographicException.cs +++ b/src/mscorlib/shared/System/Security/CryptographicException.cs @@ -7,7 +7,6 @@ using System.Runtime.Serialization; namespace System.Security.Cryptography { - [Serializable] public class CryptographicException : SystemException { public CryptographicException() @@ -39,6 +38,7 @@ namespace System.Security.Cryptography protected CryptographicException(SerializationInfo info, StreamingContext context) : base(info, context) { + throw new PlatformNotSupportedException(); } } } diff --git a/src/mscorlib/shared/System/Security/SafeBSTRHandle.cs b/src/mscorlib/shared/System/Security/SafeBSTRHandle.cs index a1164dce91..227fed3fc3 100644 --- a/src/mscorlib/shared/System/Security/SafeBSTRHandle.cs +++ b/src/mscorlib/shared/System/Security/SafeBSTRHandle.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime; using System.Diagnostics; using System.Runtime.InteropServices; @@ -25,7 +26,7 @@ namespace System.Security override protected bool ReleaseHandle() { - Interop.NtDll.ZeroMemory(handle, (UIntPtr)(Interop.OleAut32.SysStringLen(handle) * sizeof(char))); + RuntimeImports.RhZeroMemory(handle, (UIntPtr)(Interop.OleAut32.SysStringLen(handle) * sizeof(char))); Interop.OleAut32.SysFreeString(handle); return true; } @@ -36,7 +37,7 @@ namespace System.Security try { AcquirePointer(ref bufferPtr); - Interop.NtDll.ZeroMemory((IntPtr)bufferPtr, (UIntPtr)(Interop.OleAut32.SysStringLen((IntPtr)bufferPtr) * sizeof(char))); + RuntimeImports.RhZeroMemory((IntPtr)bufferPtr, (UIntPtr)(Interop.OleAut32.SysStringLen((IntPtr)bufferPtr) * sizeof(char))); } finally { diff --git a/src/mscorlib/shared/System/Security/SecureString.Unix.cs b/src/mscorlib/shared/System/Security/SecureString.Unix.cs index 0ef38e40ee..cfeebc1daf 100644 --- a/src/mscorlib/shared/System/Security/SecureString.Unix.cs +++ b/src/mscorlib/shared/System/Security/SecureString.Unix.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime; using System.Runtime.InteropServices; using System.Text; @@ -142,6 +143,41 @@ namespace System.Security _buffer.Write((ulong)(index * sizeof(char)), c); } + internal unsafe IntPtr MarshalToBSTR() + { + int length = _decryptedLength; + IntPtr ptr = IntPtr.Zero; + IntPtr result = IntPtr.Zero; + byte* bufferPtr = null; + + try + { + _buffer.AcquirePointer(ref bufferPtr); + int resultByteLength = (length + 1) * sizeof(char); + + ptr = PInvokeMarshal.AllocBSTR(length); + + Buffer.MemoryCopy(bufferPtr, (byte*)ptr, resultByteLength, length * sizeof(char)); + + result = ptr; + } + finally + { + // If we failed for any reason, free the new buffer + if (result == IntPtr.Zero && ptr != IntPtr.Zero) + { + RuntimeImports.RhZeroMemory(ptr, (UIntPtr)(length * sizeof(char))); + PInvokeMarshal.FreeBSTR(ptr); + } + + if (bufferPtr != null) + { + _buffer.ReleasePointer(); + } + } + return result; + } + internal unsafe IntPtr MarshalToStringCore(bool globalAlloc, bool unicode) { int length = _decryptedLength; @@ -179,7 +215,7 @@ namespace System.Security // release the string if we had one. if (stringPtr != IntPtr.Zero && result == IntPtr.Zero) { - UnmanagedBuffer.ZeroMemory((byte*)stringPtr, (ulong)(length * sizeof(char))); + RuntimeImports.RhZeroMemory(stringPtr, (UIntPtr)(length * sizeof(char))); MarshalFree(stringPtr, globalAlloc); } @@ -241,7 +277,7 @@ namespace System.Security try { AcquirePointer(ref ptr); - ZeroMemory(ptr, ByteLength); + RuntimeImports.RhZeroMemory((IntPtr)ptr, (UIntPtr)ByteLength); } finally { @@ -284,12 +320,6 @@ namespace System.Security Marshal.FreeHGlobal(handle); return true; } - - internal static unsafe void ZeroMemory(byte* ptr, ulong len) - { - for (ulong i = 0; i < len; i++) *ptr++ = 0; - } } - } } diff --git a/src/mscorlib/shared/System/Security/SecureString.Windows.cs b/src/mscorlib/shared/System/Security/SecureString.Windows.cs index 13f75a37e9..2a80081912 100644 --- a/src/mscorlib/shared/System/Security/SecureString.Windows.cs +++ b/src/mscorlib/shared/System/Security/SecureString.Windows.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime; using System.Runtime.InteropServices; using System.Security.Cryptography; using Microsoft.Win32; @@ -157,11 +158,7 @@ namespace System.Security _buffer.AcquirePointer(ref bufferPtr); int resultByteLength = (length + 1) * sizeof(char); - ptr = Interop.OleAut32.SysAllocStringLen(null, length); - if (ptr == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } + ptr = PInvokeMarshal.AllocBSTR(length); Buffer.MemoryCopy(bufferPtr, (byte*)ptr, resultByteLength, length * sizeof(char)); @@ -174,8 +171,8 @@ namespace System.Security // 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))); - Interop.OleAut32.SysFreeString(ptr); + RuntimeImports.RhZeroMemory(ptr, (UIntPtr)(length * sizeof(char))); + PInvokeMarshal.FreeBSTR(ptr); } if (bufferPtr != null) @@ -223,7 +220,7 @@ namespace System.Security // 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))); + RuntimeImports.RhZeroMemory(ptr, (UIntPtr)(length * sizeof(char))); MarshalFree(ptr, globalAlloc); } diff --git a/src/mscorlib/shared/System/Security/SecurityException.cs b/src/mscorlib/shared/System/Security/SecurityException.cs index 86e3cd4631..538f475343 100644 --- a/src/mscorlib/shared/System/Security/SecurityException.cs +++ b/src/mscorlib/shared/System/Security/SecurityException.cs @@ -7,7 +7,6 @@ using System.Runtime.Serialization; namespace System.Security { - [Serializable] public class SecurityException : SystemException { public SecurityException() @@ -46,6 +45,7 @@ namespace System.Security protected SecurityException(SerializationInfo info, StreamingContext context) : base(info, context) { + throw new PlatformNotSupportedException(); } public override string ToString() => base.ToString(); diff --git a/src/mscorlib/shared/System/Security/VerificationException.cs b/src/mscorlib/shared/System/Security/VerificationException.cs index 9641e1aa46..ea5a75906e 100644 --- a/src/mscorlib/shared/System/Security/VerificationException.cs +++ b/src/mscorlib/shared/System/Security/VerificationException.cs @@ -6,7 +6,6 @@ using System.Runtime.Serialization; namespace System.Security { - [Serializable] public class VerificationException : SystemException { public VerificationException() @@ -30,6 +29,7 @@ namespace System.Security protected VerificationException(SerializationInfo info, StreamingContext context) : base(info, context) { + throw new PlatformNotSupportedException(); } } } |