SecureString.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. namespace System.Security
  8. {
  9. public sealed partial class SecureString : IDisposable
  10. {
  11. private const int MaxLength = 65536;
  12. private readonly object _methodLock = new object();
  13. private bool _readOnly;
  14. private int _decryptedLength;
  15. public unsafe SecureString()
  16. {
  17. InitializeSecureString(null, 0);
  18. }
  19. [CLSCompliant(false)]
  20. public unsafe SecureString(char* value, int length)
  21. {
  22. if (value == null)
  23. {
  24. throw new ArgumentNullException(nameof(value));
  25. }
  26. if (length < 0)
  27. {
  28. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum);
  29. }
  30. if (length > MaxLength)
  31. {
  32. throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_Length);
  33. }
  34. InitializeSecureString(value, length);
  35. }
  36. public int Length
  37. {
  38. get
  39. {
  40. EnsureNotDisposed();
  41. return Volatile.Read(ref _decryptedLength);
  42. }
  43. }
  44. public void AppendChar(char c)
  45. {
  46. lock (_methodLock)
  47. {
  48. EnsureNotDisposed();
  49. EnsureNotReadOnly();
  50. AppendCharCore(c);
  51. }
  52. }
  53. // clears the current contents. Only available if writable
  54. public void Clear()
  55. {
  56. lock (_methodLock)
  57. {
  58. EnsureNotDisposed();
  59. EnsureNotReadOnly();
  60. ClearCore();
  61. }
  62. }
  63. // Do a deep-copy of the SecureString
  64. public SecureString Copy()
  65. {
  66. lock (_methodLock)
  67. {
  68. EnsureNotDisposed();
  69. return new SecureString(this);
  70. }
  71. }
  72. public void Dispose()
  73. {
  74. lock (_methodLock)
  75. {
  76. DisposeCore();
  77. }
  78. }
  79. public void InsertAt(int index, char c)
  80. {
  81. lock (_methodLock)
  82. {
  83. if (index < 0 || index > _decryptedLength)
  84. {
  85. throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexString);
  86. }
  87. EnsureNotDisposed();
  88. EnsureNotReadOnly();
  89. InsertAtCore(index, c);
  90. }
  91. }
  92. public bool IsReadOnly()
  93. {
  94. EnsureNotDisposed();
  95. return Volatile.Read(ref _readOnly);
  96. }
  97. public void MakeReadOnly()
  98. {
  99. EnsureNotDisposed();
  100. Volatile.Write(ref _readOnly, true);
  101. }
  102. public void RemoveAt(int index)
  103. {
  104. lock (_methodLock)
  105. {
  106. EnsureNotDisposed();
  107. EnsureNotReadOnly();
  108. if (index < 0 || index >= _decryptedLength)
  109. {
  110. throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexString);
  111. }
  112. RemoveAtCore(index);
  113. }
  114. }
  115. public void SetAt(int index, char c)
  116. {
  117. lock (_methodLock)
  118. {
  119. if (index < 0 || index >= _decryptedLength)
  120. {
  121. throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexString);
  122. }
  123. Debug.Assert(index <= int.MaxValue / sizeof(char));
  124. EnsureNotDisposed();
  125. EnsureNotReadOnly();
  126. SetAtCore(index, c);
  127. }
  128. }
  129. private void EnsureNotReadOnly()
  130. {
  131. if (_readOnly)
  132. {
  133. throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
  134. }
  135. }
  136. private void EnsureNotDisposed()
  137. {
  138. if (_buffer == null)
  139. {
  140. throw new ObjectDisposedException(GetType().Name);
  141. }
  142. }
  143. internal IntPtr MarshalToBSTR()
  144. {
  145. lock (_methodLock)
  146. {
  147. EnsureNotDisposed();
  148. return MarshalToBSTRCore();
  149. }
  150. }
  151. internal unsafe IntPtr MarshalToString(bool globalAlloc, bool unicode)
  152. {
  153. lock (_methodLock)
  154. {
  155. EnsureNotDisposed();
  156. return MarshalToStringCore(globalAlloc, unicode);
  157. }
  158. }
  159. private static void MarshalFree(IntPtr ptr, bool globalAlloc)
  160. {
  161. if (globalAlloc)
  162. {
  163. Marshal.FreeHGlobal(ptr);
  164. }
  165. else
  166. {
  167. Marshal.FreeCoTaskMem(ptr);
  168. }
  169. }
  170. }
  171. }