Merged into single file, added assertions
[mono.git] / mcs / class / corlib / System.Security / SecureString.cs
index bc9f579e48b886d828427e29ca673f1d83824f07..7b1d142ac1de0037dc2b5cd21fcac92c85a321b9 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System.Globalization;
+using System.Reflection;
+using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Runtime.ConstrainedExecution;
 using System.Security.Cryptography;
 using System.Security.Permissions;
+#if NET_4_0
+using System.Runtime.ExceptionServices;
+#endif
 
 namespace System.Security {
 
        [MonoTODO ("work in progress - encryption is missing")]
-       public sealed class SecureString : CriticalFinalizerObject, IDisposable {
+       public sealed class SecureString : IDisposable {
 
                private const int BlockSize = 16;
                private const int MaxSize = 65536;
@@ -88,6 +91,9 @@ namespace System.Security {
                        }
                }
 
+#if NET_4_0
+               [HandleProcessCorruptedStateExceptions]
+#endif
                public void AppendChar (char c)
                {
                        if (disposed)
@@ -128,6 +134,7 @@ namespace System.Security {
                {
                        SecureString ss = new SecureString ();
                        ss.data = (byte[]) data.Clone ();
+                       ss.length = length;
                        return ss;
                }
 
@@ -143,6 +150,9 @@ namespace System.Security {
                        length = 0;
                }
 
+#if NET_4_0
+               [HandleProcessCorruptedStateExceptions]
+#endif
                public void InsertAt (int index, char c)
                {
                        if (disposed)
@@ -163,7 +173,7 @@ namespace System.Security {
                                Decrypt ();
                                Alloc (++length, true);
                                int n = index * 2;
-                               Buffer.BlockCopy (data, n, data, n + 2, data.Length - 2);
+                               Buffer.BlockCopy (data, n, data, n + 2, data.Length - n - 2);
                                data[n++] = (byte) (c >> 8);
                                data[n] = (byte) c;
                        }
@@ -184,6 +194,9 @@ namespace System.Security {
                        read_only = true;
                }
 
+#if NET_4_0
+               [HandleProcessCorruptedStateExceptions]
+#endif
                public void RemoveAt (int index)
                {
                        if (disposed)
@@ -205,6 +218,9 @@ namespace System.Security {
                        }
                }
 
+#if NET_4_0
+               [HandleProcessCorruptedStateExceptions]
+#endif
                public void SetAt (int index, char c)
                {
                        if (disposed)
@@ -229,19 +245,31 @@ namespace System.Security {
 
                // internal/private stuff
 
-               [MonoTODO ("ProtectedMemory is in System.Security.dll - move this into the runtime/icall")]
+//             [MethodImplAttribute(MethodImplOptions.InternalCall)]
+//             extern static void EncryptInternal (byte [] data, object scope);
+
+//             [MethodImplAttribute(MethodImplOptions.InternalCall)]
+//             extern static void DecryptInternal (byte [] data, object scope);
+
+//             static readonly object scope = Enum.Parse (
+//                     Assembly.Load (Consts.AssemblySystem_Security)
+//                     .GetType ("System.Security.Cryptography.MemoryProtectionScope"), "SameProcess");
+
+               // Note that ProtectedMemory is not supported on non-Windows environment right now.
                private void Encrypt ()
                {
                        if ((data != null) && (data.Length > 0)) {
-//                             ProtectedMemory.Protect (data, MemoryProtectionScope.SameProcess);
+                               // It somehow causes nunit test breakage
+                               // EncryptInternal (data, scope);
                        }
                }
 
-               [MonoTODO ("ProtectedMemory is in System.Security.dll - move this into the runtime/icall")]
+               // Note that ProtectedMemory is not supported on non-Windows environment right now.
                private void Decrypt ()
                {
                        if ((data != null) && (data.Length > 0)) {
-//                             ProtectedMemory.Unprotect (data, MemoryProtectionScope.SameProcess);
+                               // It somehow causes nunit test breakage
+                               // DecryptInternal (data, scope);
                        }
                }
 
@@ -289,5 +317,3 @@ namespace System.Security {
                }
        }
 }
-
-#endif