Skip to content

Commit 21f2eac

Browse files
committed
Check there is no trailing data in Asn1Object.FromByteArray
1 parent 938f657 commit 21f2eac

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

crypto/src/asn1/Asn1Object.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ public abstract class Asn1Object
1313
public static Asn1Object FromByteArray(
1414
byte[] data)
1515
{
16-
try
16+
try
1717
{
18-
return new Asn1InputStream(data).ReadObject();
18+
MemoryStream input = new MemoryStream(data, false);
19+
Asn1InputStream asn1 = new Asn1InputStream(input, data.Length);
20+
Asn1Object result = asn1.ReadObject();
21+
if (input.Position != input.Length)
22+
throw new IOException("extra data found after object");
23+
return result;
1924
}
2025
catch (InvalidCastException)
2126
{
22-
throw new IOException("cannot recognise object in stream");
27+
throw new IOException("cannot recognise object in byte array");
2328
}
2429
}
2530

@@ -36,7 +41,7 @@ public static Asn1Object FromStream(
3641
}
3742
catch (InvalidCastException)
3843
{
39-
throw new IOException("cannot recognise object in stream");
44+
throw new IOException("cannot recognise object in stream");
4045
}
4146
}
4247

crypto/src/crypto/tls/TlsUtilities.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,12 @@ public static int ReadVersionRaw(Stream input)
529529

530530
public static Asn1Object ReadAsn1Object(byte[] encoding)
531531
{
532-
Asn1InputStream asn1 = new Asn1InputStream(encoding);
532+
MemoryStream input = new MemoryStream(encoding, false);
533+
Asn1InputStream asn1 = new Asn1InputStream(input, encoding.Length);
533534
Asn1Object result = asn1.ReadObject();
534535
if (null == result)
535536
throw new TlsFatalAlert(AlertDescription.decode_error);
536-
if (null != asn1.ReadObject())
537+
if (input.Position != input.Length)
537538
throw new TlsFatalAlert(AlertDescription.decode_error);
538539
return result;
539540
}

crypto/test/src/asn1/test/TagTest.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23

34
using NUnit.Framework;
45

@@ -33,14 +34,17 @@ public override string Name
3334

3435
public override void PerformTest()
3536
{
36-
DerApplicationSpecific app = (DerApplicationSpecific)
37-
Asn1Object.FromByteArray(longTagged);
37+
Asn1InputStream aIn = new Asn1InputStream(longTagged);
3838

39-
app = (DerApplicationSpecific) Asn1Object.FromByteArray(app.GetContents());
39+
DerApplicationSpecific app = (DerApplicationSpecific)aIn.ReadObject();
4040

41-
Asn1InputStream aIn = new Asn1InputStream(app.GetContents());
41+
aIn = new Asn1InputStream(app.GetContents());
4242

43-
Asn1TaggedObject tagged = (Asn1TaggedObject) aIn.ReadObject();
43+
app = (DerApplicationSpecific)aIn.ReadObject();
44+
45+
aIn = new Asn1InputStream(app.GetContents());
46+
47+
Asn1TaggedObject tagged = (Asn1TaggedObject)aIn.ReadObject();
4448

4549
if (tagged.TagNo != 32)
4650
{

crypto/test/src/openssl/test/ReaderTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public override void PerformTest()
198198
doDudPasswordTest("3ee7a8", 10, "DER length more than 4 bytes: 57");
199199
doDudPasswordTest("41af75", 11, "unknown tag 16 encountered");
200200
doDudPasswordTest("1704a5", 12, "corrupted stream detected");
201-
doDudPasswordTest("1c5822", 13, "Unknown object in GetInstance: Org.BouncyCastle.Asn1.DerUtf8String");
201+
doDudPasswordTest("1c5822", 13, "extra data found after object");
202202
doDudPasswordTest("5a3d16", 14, "corrupted stream detected");
203203
doDudPasswordTest("8d0c97", 15, "corrupted stream detected");
204204
doDudPasswordTest("bc0daf", 16, "corrupted stream detected");
@@ -342,7 +342,7 @@ private void doDudPasswordTest(string password, int index, string message)
342342

343343
Fail("issue not detected: " + index);
344344
}
345-
catch (IOException e)
345+
catch (Exception e)
346346
{
347347
if (e.Message.IndexOf(message) < 0)
348348
{

0 commit comments

Comments
 (0)