diff --git a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs index 0e706800632e..e8a2db985363 100644 --- a/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs +++ b/src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs @@ -35,6 +35,7 @@ public CryptographicException(string format, string insert) { } public partial class CryptoStream : System.IO.Stream, System.IDisposable { public CryptoStream(System.IO.Stream stream, System.Security.Cryptography.ICryptoTransform transform, System.Security.Cryptography.CryptoStreamMode mode) { } + public CryptoStream(System.IO.Stream stream, System.Security.Cryptography.ICryptoTransform transform, System.Security.Cryptography.CryptoStreamMode mode, bool leaveOpen) { } public override bool CanRead { get { return default(bool); } } public override bool CanSeek { get { return default(bool); } } public override bool CanWrite { get { return default(bool); } } diff --git a/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs b/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs index db81acde2f9f..d51ec1e9bdca 100644 --- a/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs +++ b/src/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs @@ -25,15 +25,22 @@ public class CryptoStream : Stream, IDisposable private bool _canWrite; private bool _finalBlockTransformed; private SemaphoreSlim _lazyAsyncActiveSemaphore; - + private readonly bool _leaveOpen; + // Constructors public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode) + : this(stream, transform, mode, false) + { + } + + public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode, bool leaveOpen) { _stream = stream; _transformMode = mode; _transform = transform; + _leaveOpen = leaveOpen; switch (_transformMode) { case CryptoStreamMode.Read: @@ -523,7 +530,10 @@ protected override void Dispose(bool disposing) { FlushFinalBlock(); } - _stream.Dispose(); + if (!_leaveOpen) + { + _stream.Dispose(); + } } } finally diff --git a/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs b/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs index e99e7891dd21..424586076ac6 100644 --- a/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs +++ b/src/System.Security.Cryptography.Primitives/tests/CryptoStream.cs @@ -162,10 +162,35 @@ public static void NestedCryptoStreams() public static void MultipleDispose() { ICryptoTransform encryptor = new IdentityTransform(1, 1, true); + + using (MemoryStream output = new MemoryStream()) + { + using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write)) + { + encryptStream.Dispose(); + } + + Assert.Equal(false, output.CanRead); + } + + using (MemoryStream output = new MemoryStream()) + { + using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write, leaveOpen: false)) + { + encryptStream.Dispose(); + } + + Assert.Equal(false, output.CanRead); + } + using (MemoryStream output = new MemoryStream()) - using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write)) { - encryptStream.Dispose(); + using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write, leaveOpen: true)) + { + encryptStream.Dispose(); + } + + Assert.Equal(true, output.CanRead); } }