summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-10-13 23:57:48 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-10-13 23:57:48 +0700
commitcefa1317c853c3f6d7eaf669771c3bf60454a921 (patch)
tree30bc89f6cfb2877766288bf015eda288a19e2bde
parentDER sequence/set encoding opts. (diff)
downloadBouncyCastle.NET-ed25519-cefa1317c853c3f6d7eaf669771c3bf60454a921.tar.xz
Improve ASN.1 set special handling
-rw-r--r--crypto/src/asn1/Asn1Encodable.cs50
1 files changed, 27 insertions, 23 deletions
diff --git a/crypto/src/asn1/Asn1Encodable.cs b/crypto/src/asn1/Asn1Encodable.cs
index 6314bf76f..f42f0bbcf 100644
--- a/crypto/src/asn1/Asn1Encodable.cs
+++ b/crypto/src/asn1/Asn1Encodable.cs
@@ -18,35 +18,39 @@ namespace Org.BouncyCastle.Asn1
 			return bOut.ToArray();
         }
 
-		public byte[] GetEncoded(
-			string encoding)
-		{
-			if (encoding.Equals(Der))
-			{
-				MemoryStream bOut = new MemoryStream();
-				DerOutputStream dOut = new DerOutputStream(bOut);
-
-				if (this is Asn1Set)
-				{
-					dOut.WriteObject(new DerSet((this as Asn1Set).elements));
-				}
-				else
-				{
-					dOut.WriteObject(this);
-				}
-
-				return bOut.ToArray();
-			}
+        public byte[] GetEncoded(string encoding)
+        {
+            if (encoding.Equals(Der))
+            {
+                MemoryStream bOut = new MemoryStream();
+                DerOutputStream dOut = new DerOutputStream(bOut);
 
-			return GetEncoded();
-		}
+                Asn1Object asn1Object = ToAsn1Object();
+
+                Asn1Set asn1Set = asn1Object as Asn1Set;
+                if (null != asn1Set)
+                {
+                    /*
+                     * NOTE: Even a DerSet isn't necessarily already in sorted order (particularly from DerSetParser),
+                     * so all sets have to be converted here.
+                     */
+                    asn1Object = new DerSet(asn1Set.elements);
+                }
+
+                asn1Object.Encode(dOut);
+
+                return bOut.ToArray();
+            }
+
+            return GetEncoded();
+        }
 
-		/**
+        /**
 		* Return the DER encoding of the object, null if the DER encoding can not be made.
 		*
 		* @return a DER byte array, null otherwise.
 		*/
-		public byte[] GetDerEncoded()
+        public byte[] GetDerEncoded()
 		{
 			try
 			{