summary refs log tree commit diff
path: root/crypto/src/asn1/BerOctetString.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/BerOctetString.cs')
-rw-r--r--crypto/src/asn1/BerOctetString.cs72
1 files changed, 40 insertions, 32 deletions
diff --git a/crypto/src/asn1/BerOctetString.cs b/crypto/src/asn1/BerOctetString.cs
index 0dab617b9..52ddd51c3 100644
--- a/crypto/src/asn1/BerOctetString.cs
+++ b/crypto/src/asn1/BerOctetString.cs
@@ -120,52 +120,60 @@ namespace Org.BouncyCastle.Asn1
 			return GetEnumerator();
 		}
 
-        internal override bool EncodeConstructed()
+        internal override bool EncodeConstructed(int encoding)
         {
-            // NOTE: Assumes BER encoding
+            if (Asn1OutputStream.EncodingBer != encoding)
+                return base.EncodeConstructed(encoding);
+
             return null != elements || contents.Length > segmentLimit;
         }
 
-        internal override int EncodedLength(bool withID)
+        internal override int EncodedLength(int encoding, bool withID)
         {
-            throw Platform.CreateNotImplementedException("BerOctetString.EncodedLength");
-
-            // TODO This depends on knowing it's not DER
-            //if (!EncodeConstructed())
-            //    return EncodedLength(withID, contents.Length);
-
-            //int totalLength = withID ? 4 : 3;
-
-            //if (null != elements)
-            //{
-            //    for (int i = 0; i < elements.Length; ++i)
-            //    {
-            //        totalLength += elements[i].EncodedLength(true);
-            //    }
-            //}
-            //else
-            //{
-            //    int fullSegments = contents.Length / segmentLimit;
-            //    totalLength += fullSegments * EncodedLength(true, segmentLimit);
-
-            //    int lastSegmentLength = contents.Length - (fullSegments * segmentLimit);
-            //    if (lastSegmentLength > 0)
-            //    {
-            //        totalLength += EncodedLength(true, lastSegmentLength);
-            //    }
-            //}
-
-            //return totalLength;
+            if (Asn1OutputStream.EncodingBer != encoding)
+                return base.EncodedLength(encoding, withID);
+
+            if (!EncodeConstructed(encoding))
+                return EncodedLength(withID, contents.Length);
+
+            int totalLength = withID ? 4 : 3;
+
+            if (null != elements)
+            {
+                for (int i = 0; i < elements.Length; ++i)
+                {
+                    totalLength += elements[i].EncodedLength(encoding, true);
+                }
+            }
+            else
+            {
+                int fullSegments = contents.Length / segmentLimit;
+                totalLength += fullSegments * EncodedLength(true, segmentLimit);
+
+                int lastSegmentLength = contents.Length - (fullSegments * segmentLimit);
+                if (lastSegmentLength > 0)
+                {
+                    totalLength += EncodedLength(true, lastSegmentLength);
+                }
+            }
+
+            return totalLength;
         }
 
         internal override void Encode(Asn1OutputStream asn1Out, bool withID)
         {
-            if (!asn1Out.IsBer || !EncodeConstructed())
+            if (Asn1OutputStream.EncodingBer != asn1Out.Encoding)
             {
                 base.Encode(asn1Out, withID);
                 return;
             }
 
+            if (!EncodeConstructed(asn1Out.Encoding))
+            {
+                Encode(asn1Out, withID, contents, 0, contents.Length);
+                return;
+            }
+
             asn1Out.WriteIdentifier(withID, Asn1Tags.Constructed | Asn1Tags.OctetString);
             asn1Out.WriteByte(0x80);