summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-10-13 17:54:05 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-10-13 17:54:05 +0700
commit2e6b7d2714818494e094209d11e9184dd370ad18 (patch)
tree3c41ee64a55f1eacdc8d48825760ed012a0c9885
parentLatest ArmoredInputStream stuff from bc-java (diff)
downloadBouncyCastle.NET-ed25519-2e6b7d2714818494e094209d11e9184dd370ad18.tar.xz
DER sequence/set encoding opts.
-rw-r--r--crypto/src/asn1/DerSequence.cs42
-rw-r--r--crypto/src/asn1/DerSet.cs42
2 files changed, 56 insertions, 28 deletions
diff --git a/crypto/src/asn1/DerSequence.cs b/crypto/src/asn1/DerSequence.cs
index 823fa869b..7940f6b5a 100644
--- a/crypto/src/asn1/DerSequence.cs
+++ b/crypto/src/asn1/DerSequence.cs
@@ -45,7 +45,7 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-		/*
+        /*
 		 * A note on the implementation:
 		 * <p>
 		 * As Der requires the constructed, definite-length model to
@@ -53,22 +53,36 @@ namespace Org.BouncyCastle.Asn1
 		 * ASN.1 descriptions given. Rather than just outputing Sequence,
 		 * we also have to specify Constructed, and the objects length.
 		 */
-		internal override void Encode(DerOutputStream derOut)
-		{
-			// TODO Intermediate buffer could be avoided if we could calculate expected length
-			MemoryStream bOut = new MemoryStream();
-			DerOutputStream dOut = new DerOutputStream(bOut);
+        internal override void Encode(DerOutputStream derOut)
+        {
+            if (Count < 1)
+            {
+                derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, Asn1OctetString.EmptyOctets);
+                return;
+            }
 
-			foreach (Asn1Encodable obj in this)
-			{
-				dOut.WriteObject(obj);
-			}
+            // TODO Intermediate buffer could be avoided if we could calculate expected length
+            MemoryStream bOut = new MemoryStream();
+            DerOutputStream dOut = new DerOutputStream(bOut);
 
-            Platform.Dispose(dOut);
+            foreach (Asn1Encodable obj in this)
+            {
+                dOut.WriteObject(obj);
+            }
+
+            dOut.Flush();
 
+#if PORTABLE
             byte[] bytes = bOut.ToArray();
+            int length = bytes.Length;
+#else
+            byte[] bytes = bOut.GetBuffer();
+            int length = (int)bOut.Position;
+#endif
 
-			derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, bytes);
-		}
-	}
+            derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, bytes, 0, length);
+
+            Platform.Dispose(dOut);
+        }
+    }
 }
diff --git a/crypto/src/asn1/DerSet.cs b/crypto/src/asn1/DerSet.cs
index d4c242778..bf618ae46 100644
--- a/crypto/src/asn1/DerSet.cs
+++ b/crypto/src/asn1/DerSet.cs
@@ -62,7 +62,7 @@ namespace Org.BouncyCastle.Asn1
 			}
 		}
 
-		/*
+        /*
 		 * A note on the implementation:
 		 * <p>
 		 * As Der requires the constructed, definite-length model to
@@ -70,22 +70,36 @@ namespace Org.BouncyCastle.Asn1
 		 * ASN.1 descriptions given. Rather than just outputing Set,
 		 * we also have to specify Constructed, and the objects length.
 		 */
-		internal override void Encode(DerOutputStream derOut)
-		{
-			// TODO Intermediate buffer could be avoided if we could calculate expected length
-			MemoryStream bOut = new MemoryStream();
-			DerOutputStream dOut = new DerOutputStream(bOut);
+        internal override void Encode(DerOutputStream derOut)
+        {
+            if (Count < 1)
+            {
+                derOut.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, Asn1OctetString.EmptyOctets);
+                return;
+            }
 
-			foreach (Asn1Encodable obj in this)
-			{
-				dOut.WriteObject(obj);
-			}
+            // TODO Intermediate buffer could be avoided if we could calculate expected length
+            MemoryStream bOut = new MemoryStream();
+            DerOutputStream dOut = new DerOutputStream(bOut);
 
-            Platform.Dispose(dOut);
+            foreach (Asn1Encodable obj in this)
+            {
+                dOut.WriteObject(obj);
+            }
 
+            dOut.Flush();
+
+#if PORTABLE
             byte[] bytes = bOut.ToArray();
+            int length = bytes.Length;
+#else
+            byte[] bytes = bOut.GetBuffer();
+            int length = (int)bOut.Position;
+#endif
 
-			derOut.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, bytes);
-		}
-	}
+            derOut.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, bytes, 0, length);
+
+            Platform.Dispose(dOut);
+        }
+    }
 }