runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Out of bounds error with CryptoStream.Write using FromBase64Transform

Open freefalling opened this issue 7 years ago • 6 comments

The following code worked fine in .net framework 4.7.1, but in .net core 2.1/2.2/3.0 throws an ArgumentException Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection on the CryptoStream.Write method.

public byte[] DecryptBase64(byte[] data)
{
     FromBase64Transform tBase64 = new FromBase64Transform();
     MemoryStream streamDecrypted = new MemoryStream();
     CryptoStream stream = new CryptoStream(streamDecrypted, tBase64, CryptoStreamMode.Write);

     stream.Write(data, 0, data.Length);
     stream.FlushFinalBlock();
     stream.Close();

     return streamDecrypted.ToArray();
}

The data param has 2316 elements (0-2315), the offset is 0 and length is 2316. It does not make any sense to me an out of bounds error when calling stream.Write(data, 0, data.Length).

This issue seems to be related to dotnet/runtime#23550 and dotnet/runtime#23206, the problem for my team is that we can't make use of the DES.Create() workaround because, as you can see, we are using FromBase64Transform

freefalling avatar Sep 27 '19 14:09 freefalling

I am not able to reproduce this with the 3.0 released build:

byte[] data = new byte[2316];
data.AsSpan().Fill((byte)'A');

FromBase64Transform tBase64 = new FromBase64Transform();
MemoryStream streamDecrypted = new MemoryStream();
CryptoStream stream = new CryptoStream(streamDecrypted, tBase64, CryptoStreamMode.Write);

stream.Write(data, 0, data.Length);
stream.FlushFinalBlock();
stream.Close();

Console.WriteLine(BitConverter.ToString(streamDecrypted.ToArray()));

Works for me (prints a lot of zeros). Filling it with spaces also works, and prints nothing (the empty array was produced).

If you can't share sample data that reproduces the problem, it would at least help to have a callstack from the call to CryptoStream.Write through the throw.

bartonjs avatar Sep 28 '19 00:09 bartonjs

Changing the application to 2.1 does reproduce the error. So it just looks like this is something that has already been fixed for 3.0.

bartonjs avatar Sep 28 '19 00:09 bartonjs

In .NET Core 2.1. the exception is thrown at https://github.com/dotnet/corefx/blob/7ee84596d92e178bce54c986df31ccc52479e772/src/System.Security.Cryptography.Encoding/src/System/Security/Cryptography/Base64Transforms.cs#L131

grafik

The outputBuffer is too small, hence the crash / exception. This was fixed in https://github.com/dotnet/corefx/commit/ba9b702d10dae9ac9360f8ae7b4f7dcda01f0f73 (for .NET Core 3.0).

gfoidl avatar Sep 28 '19 10:09 gfoidl

Yes, I can confirm that it's working on 3.0. Is there any workaround for .NET Core 2.1?

freefalling avatar Oct 07 '19 20:10 freefalling

(Sorry for it being a long time)

It looks like the bug was introduced in the original port to .NET Core, the only workaround that I can think of for 2.1 is to pre-filter whitespace out of your input before letting the FromBase64Transform process it. The probably easier answer is to upgrade to 3.1, since the bug is fixed in that release.

bartonjs avatar Jul 07 '20 16:07 bartonjs

.NET Core 2.1 is no longer in support, and no supported versions have this error, closing.

bartonjs avatar Jul 06 '22 19:07 bartonjs