summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-02-19 21:03:22 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-02-19 21:03:22 +0700
commitad0b6c99d34da50d5473a9c14837a9ce199d0200 (patch)
treefbe5e81540f15b7a3cca0401380d33bd9fbb9cc6 /crypto/test
parentCase-insensitive check of boolean env. var. (diff)
downloadBouncyCastle.NET-ed25519-ad0b6c99d34da50d5473a9c14837a9ce199d0200.tar.xz
ASN.1 updates from bc-java
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/src/asn1/test/InputStreamTest.cs63
-rw-r--r--crypto/test/src/openssl/test/ReaderTest.cs6
2 files changed, 59 insertions, 10 deletions
diff --git a/crypto/test/src/asn1/test/InputStreamTest.cs b/crypto/test/src/asn1/test/InputStreamTest.cs
index 1d92759e0..4cfb304d1 100644
--- a/crypto/test/src/asn1/test/InputStreamTest.cs
+++ b/crypto/test/src/asn1/test/InputStreamTest.cs
@@ -3,6 +3,7 @@ using System.IO;
 
 using NUnit.Framework;
 
+using Org.BouncyCastle.Utilities.Encoders;
 using Org.BouncyCastle.Utilities.Test;
 
 namespace Org.BouncyCastle.Asn1.Tests
@@ -11,9 +12,15 @@ namespace Org.BouncyCastle.Asn1.Tests
 	public class InputStreamTest
 		: SimpleTest
 	{
-		private static readonly byte[] outOfBoundsLength = new byte[] { (byte)0x30, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff };
-		private static readonly byte[] negativeLength = new byte[] { (byte)0x30, (byte)0x84, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff };
-		private static readonly byte[] outsideLimitLength = new byte[] { (byte)0x30, (byte)0x83, (byte)0x0f, (byte)0xff, (byte)0xff };
+        private static readonly byte[] outOfBoundsLength = new byte[] { (byte)0x30, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff };
+        private static readonly byte[] negativeLength = new byte[] { (byte)0x30, (byte)0x84, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff };
+        private static readonly byte[] outsideLimitLength = new byte[] { (byte)0x30, (byte)0x83, (byte)0x0f, (byte)0xff, (byte)0xff };
+
+        private static readonly byte[] classCast1 = Base64.Decode("p1AkHmYAvfOEIrL4ESfrNg==");
+        private static readonly byte[] classCast2 = Base64.Decode("JICNbaBUTTq7uxj5mg==");
+        private static readonly byte[] classCast3 = Base64.Decode("JAKzADNCxhrrBSVS");
+        private static readonly byte[] memoryError1 = Base64.Decode("vm66gOiEe+FV/NvujMwSkUp5Lffw5caQlaRU5sdMPC70IGWmyK2/");
+        private static readonly byte[] memoryError2 = Base64.Decode("vm4ogOSEfVGsS3w+KTzb2A0ALYR8VBOQqQeuRwnsPC4AAGWEDLjd");
 
 		public override string Name
 		{
@@ -46,7 +53,7 @@ namespace Org.BouncyCastle.Asn1.Tests
 			}
 			catch (IOException e)
 			{
-				if (!e.Message.Equals("Corrupted stream - negative length found"))
+				if (!e.Message.Equals("corrupted stream - negative length found"))
 				{
 					Fail("wrong exception: " + e.Message);
 				}
@@ -61,12 +68,54 @@ namespace Org.BouncyCastle.Asn1.Tests
 			}
 			catch (IOException e)
 			{
-				if (!e.Message.Equals("Corrupted stream - out of bounds length found"))
-				{
+                if (!e.Message.Equals("corrupted stream - out of bounds length found: 1048575 >= 5"))
+                {
 					Fail("wrong exception: " + e.Message);
 				}
 			}
-		}
+
+            DoTestWithByteArray(classCast1, "unknown object encountered: Org.BouncyCastle.Asn1.DerApplicationSpecific");
+            DoTestWithByteArray(classCast2, "unknown object encountered: Org.BouncyCastle.Asn1.BerTaggedObjectParser");
+            DoTestWithByteArray(classCast3, "unknown object encountered in constructed OCTET STRING: Org.BouncyCastle.Asn1.DerTaggedObject");
+
+            DoTestWithByteArray(memoryError1, "corrupted stream - out of bounds length found: 2078365180 >= 39");
+            DoTestWithByteArray(memoryError2, "corrupted stream - out of bounds length found: 2102504523 >= 39");
+        }
+
+        private void DoTestWithByteArray(byte[] data, string message)
+        {
+            try
+            {
+                Asn1InputStream input = new Asn1InputStream(data);
+
+                IAsn1Convertible p;
+                while ((p = input.ReadObject()) != null)
+                {
+                    Asn1Sequence asn1 = Asn1Sequence.GetInstance(p);
+                    for (int i = 0; i < asn1.Count; i++)
+                    {
+                        IAsn1Convertible c = asn1[i];
+                    }
+                }
+            }
+            catch (IOException e)
+            {
+                IsEquals(e.Message, message, e.Message);
+            }
+            // TODO Without InMemoryRepresentable, the IOException may be swapped/wrapped with an Asn1ParsingException
+            catch (Asn1ParsingException e)
+            {
+                Exception messageException = e;
+
+                IOException ioe = e.InnerException as IOException;
+                if (ioe != null)
+                {
+                    messageException = ioe;
+                }
+
+                IsEquals(messageException.Message, message, messageException.Message);
+            }
+        }
 
 		public static void Main(
 			string[] args)
diff --git a/crypto/test/src/openssl/test/ReaderTest.cs b/crypto/test/src/openssl/test/ReaderTest.cs
index c0be5c848..d0bb3661b 100644
--- a/crypto/test/src/openssl/test/ReaderTest.cs
+++ b/crypto/test/src/openssl/test/ReaderTest.cs
@@ -185,8 +185,8 @@ namespace Org.BouncyCastle.OpenSsl.Tests
             doOpenSslDsaTest("rc2_64_cbc");
             doOpenSslRsaTest("rc2_64_cbc");
 
-            doDudPasswordTest("7fd98", 0, "Corrupted stream - out of bounds length found");
-            doDudPasswordTest("ef677", 1, "Corrupted stream - out of bounds length found");
+            doDudPasswordTest("7fd98", 0, "corrupted stream - out of bounds length found: 599005160 >= 447");
+            doDudPasswordTest("ef677", 1, "corrupted stream - out of bounds length found: 2087569732 >= 447");
             doDudPasswordTest("800ce", 2, "unknown tag 26 encountered");
             doDudPasswordTest("b6cd8", 3, "DEF length 81 object truncated by 56");
             doDudPasswordTest("28ce09", 4, "DEF length 110 object truncated by 28");
@@ -202,7 +202,7 @@ namespace Org.BouncyCastle.OpenSsl.Tests
             doDudPasswordTest("5a3d16", 14, "corrupted stream detected");
             doDudPasswordTest("8d0c97", 15, "corrupted stream detected");
             doDudPasswordTest("bc0daf", 16, "corrupted stream detected");
-            doDudPasswordTest("aaf9c4d",17, "Corrupted stream - out of bounds length found");
+            doDudPasswordTest("aaf9c4d", 17, "corrupted stream - out of bounds length found: 1580418590 >= 447");
 
             // encrypted private key test
             pGet = new Password("password".ToCharArray());