summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-09 16:27:36 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-09 16:27:36 +0700
commit4c3c4694b6331625eee6e9ec1c92b0d17c6db7bf (patch)
tree06b3db5d6ccfc7cd4259c45d947e0a5d97a450dd
parentFix deprecated call (diff)
downloadBouncyCastle.NET-ed25519-4c3c4694b6331625eee6e9ec1c92b0d17c6db7bf.tar.xz
Fix DER encoding of lazy objects
-rw-r--r--crypto/src/asn1/LazyDLSequence.cs16
-rw-r--r--crypto/src/asn1/LazyDLSet.cs16
2 files changed, 24 insertions, 8 deletions
diff --git a/crypto/src/asn1/LazyDLSequence.cs b/crypto/src/asn1/LazyDLSequence.cs
index fe8e71794..63152e446 100644
--- a/crypto/src/asn1/LazyDLSequence.cs
+++ b/crypto/src/asn1/LazyDLSequence.cs
@@ -65,6 +65,7 @@ namespace Org.BouncyCastle.Asn1
 
         internal override int EncodedLength(bool withID)
         {
+            // TODO This depends on knowing it's BER
             byte[] encoded = GetContents();
             if (encoded != null)
             {
@@ -76,11 +77,18 @@ namespace Org.BouncyCastle.Asn1
 
         internal override void Encode(Asn1OutputStream asn1Out, bool withID)
         {
-            byte[] encoded = GetContents();
-            if (encoded != null)
+            if (asn1Out.IsBer)
             {
-                asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Sequence, encoded);
-                return;
+                byte[] encoded = GetContents();
+                if (encoded != null)
+                {
+                    asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Sequence, encoded);
+                    return;
+                }
+            }
+            else
+            {
+                Force();
             }
 
             base.Encode(asn1Out, withID);
diff --git a/crypto/src/asn1/LazyDLSet.cs b/crypto/src/asn1/LazyDLSet.cs
index f644f69e5..0d4d84310 100644
--- a/crypto/src/asn1/LazyDLSet.cs
+++ b/crypto/src/asn1/LazyDLSet.cs
@@ -65,6 +65,7 @@ namespace Org.BouncyCastle.Asn1
 
         internal override int EncodedLength(bool withID)
         {
+            // TODO This depends on knowing it's BER
             byte[] encoded = GetContents();
             if (encoded != null)
             {
@@ -76,11 +77,18 @@ namespace Org.BouncyCastle.Asn1
 
         internal override void Encode(Asn1OutputStream asn1Out, bool withID)
         {
-            byte[] encoded = GetContents();
-            if (encoded != null)
+            if (asn1Out.IsBer)
             {
-                asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Set, encoded);
-                return;
+                byte[] encoded = GetContents();
+                if (encoded != null)
+                {
+                    asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Set, encoded);
+                    return;
+                }
+            }
+            else
+            {
+                Force();
             }
 
             base.Encode(asn1Out, withID);