DecryptedSecureString.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright 2018-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Runtime.InteropServices;
  17. using System.Security;
  18. using MongoDB.Bson.IO;
  19. using MongoDB.Driver.Core.Misc;
  20. namespace MongoDB.Driver
  21. {
  22. internal sealed class DecryptedSecureString : IDisposable
  23. {
  24. // private fields
  25. private char[] _chars;
  26. private GCHandle _charsHandle;
  27. private IntPtr _charsIntPtr;
  28. private bool _disposed;
  29. private readonly SecureString _secureString;
  30. private byte[] _utf8Bytes;
  31. private GCHandle _utf8BytesHandle;
  32. // constructors
  33. public DecryptedSecureString(SecureString secureString)
  34. {
  35. _secureString = Ensure.IsNotNull(secureString, nameof(secureString));
  36. }
  37. // finalizer
  38. ~DecryptedSecureString()
  39. {
  40. Dispose(false);
  41. }
  42. // public methods
  43. public void Dispose()
  44. {
  45. Dispose(true);
  46. GC.SuppressFinalize(this);
  47. }
  48. public char[] GetChars()
  49. {
  50. if (_chars == null)
  51. {
  52. #if NET452
  53. _charsIntPtr = Marshal.SecureStringToGlobalAllocUnicode(_secureString);
  54. #else
  55. _charsIntPtr = SecureStringMarshal.SecureStringToGlobalAllocUnicode(_secureString);
  56. #endif
  57. _chars = new char[_secureString.Length];
  58. _charsHandle = GCHandle.Alloc(_chars, GCHandleType.Pinned);
  59. Marshal.Copy(_charsIntPtr, _chars, 0, _secureString.Length);
  60. }
  61. return _chars;
  62. }
  63. public byte[] GetUtf8Bytes()
  64. {
  65. if (_utf8Bytes == null)
  66. {
  67. var chars = GetChars();
  68. var encoding = Utf8Encodings.Strict;
  69. _utf8Bytes = new byte[encoding.GetByteCount(chars)];
  70. _utf8BytesHandle = GCHandle.Alloc(_utf8Bytes, GCHandleType.Pinned);
  71. encoding.GetBytes(chars, 0, chars.Length, _utf8Bytes, 0);
  72. }
  73. return _utf8Bytes;
  74. }
  75. // private methods
  76. private void Dispose(bool disposing)
  77. {
  78. if (!_disposed)
  79. {
  80. if (disposing)
  81. {
  82. if (_utf8Bytes != null)
  83. {
  84. Array.Clear(_utf8Bytes, 0, _utf8Bytes.Length);
  85. }
  86. if (_chars != null)
  87. {
  88. Array.Clear(_chars, 0, _chars.Length);
  89. }
  90. }
  91. if (_utf8BytesHandle.IsAllocated)
  92. {
  93. _utf8BytesHandle.Free();
  94. }
  95. if (_charsHandle.IsAllocated)
  96. {
  97. _charsHandle.Free();
  98. }
  99. if (_charsIntPtr != IntPtr.Zero)
  100. {
  101. Marshal.ZeroFreeGlobalAllocUnicode(_charsIntPtr);
  102. _charsIntPtr = IntPtr.Zero; // to facilitate testing
  103. }
  104. _disposed = true;
  105. }
  106. }
  107. }
  108. }