| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- // Licensed to the .NET Foundation under one or more agreements.
- // 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.Diagnostics;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace System.Security
- {
- public sealed partial class SecureString : IDisposable
- {
- private const int MaxLength = 65536;
- private readonly object _methodLock = new object();
- private bool _readOnly;
- private int _decryptedLength;
- public unsafe SecureString()
- {
- InitializeSecureString(null, 0);
- }
- [CLSCompliant(false)]
- public unsafe SecureString(char* value, int length)
- {
- if (value == null)
- {
- throw new ArgumentNullException(nameof(value));
- }
- if (length < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum);
- }
- if (length > MaxLength)
- {
- throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_Length);
- }
- InitializeSecureString(value, length);
- }
- public int Length
- {
- get
- {
- EnsureNotDisposed();
- return Volatile.Read(ref _decryptedLength);
- }
- }
- public void AppendChar(char c)
- {
- lock (_methodLock)
- {
- EnsureNotDisposed();
- EnsureNotReadOnly();
- AppendCharCore(c);
- }
- }
- // clears the current contents. Only available if writable
- public void Clear()
- {
- lock (_methodLock)
- {
- EnsureNotDisposed();
- EnsureNotReadOnly();
- ClearCore();
- }
- }
- // Do a deep-copy of the SecureString
- public SecureString Copy()
- {
- lock (_methodLock)
- {
- EnsureNotDisposed();
- return new SecureString(this);
- }
- }
- public void Dispose()
- {
- lock (_methodLock)
- {
- DisposeCore();
- }
- }
- public void InsertAt(int index, char c)
- {
- lock (_methodLock)
- {
- if (index < 0 || index > _decryptedLength)
- {
- throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexString);
- }
- EnsureNotDisposed();
- EnsureNotReadOnly();
- InsertAtCore(index, c);
- }
- }
- public bool IsReadOnly()
- {
- EnsureNotDisposed();
- return Volatile.Read(ref _readOnly);
- }
- public void MakeReadOnly()
- {
- EnsureNotDisposed();
- Volatile.Write(ref _readOnly, true);
- }
- public void RemoveAt(int index)
- {
- lock (_methodLock)
- {
- EnsureNotDisposed();
- EnsureNotReadOnly();
- if (index < 0 || index >= _decryptedLength)
- {
- throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexString);
- }
- RemoveAtCore(index);
- }
- }
- public void SetAt(int index, char c)
- {
- lock (_methodLock)
- {
- if (index < 0 || index >= _decryptedLength)
- {
- throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexString);
- }
- Debug.Assert(index <= int.MaxValue / sizeof(char));
- EnsureNotDisposed();
- EnsureNotReadOnly();
- SetAtCore(index, c);
- }
- }
- private void EnsureNotReadOnly()
- {
- if (_readOnly)
- {
- throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
- }
- }
- private void EnsureNotDisposed()
- {
- if (_buffer == null)
- {
- throw new ObjectDisposedException(GetType().Name);
- }
- }
- internal IntPtr MarshalToBSTR()
- {
- lock (_methodLock)
- {
- EnsureNotDisposed();
- return MarshalToBSTRCore();
- }
- }
- internal unsafe IntPtr MarshalToString(bool globalAlloc, bool unicode)
- {
- lock (_methodLock)
- {
- EnsureNotDisposed();
- return MarshalToStringCore(globalAlloc, unicode);
- }
- }
- private static void MarshalFree(IntPtr ptr, bool globalAlloc)
- {
- if (globalAlloc)
- {
- Marshal.FreeHGlobal(ptr);
- }
- else
- {
- Marshal.FreeCoTaskMem(ptr);
- }
- }
- }
- }
|