summary refs log tree commit diff
path: root/crypto/src/asn1/Asn1OctetString.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/Asn1OctetString.cs')
-rw-r--r--crypto/src/asn1/Asn1OctetString.cs28
1 files changed, 25 insertions, 3 deletions
diff --git a/crypto/src/asn1/Asn1OctetString.cs b/crypto/src/asn1/Asn1OctetString.cs
index d5766e299..66d6fb8d1 100644
--- a/crypto/src/asn1/Asn1OctetString.cs
+++ b/crypto/src/asn1/Asn1OctetString.cs
@@ -58,14 +58,36 @@ namespace Org.BouncyCastle.Asn1
          */
         public static Asn1OctetString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
+            if (declaredExplicit)
+            {
+                if (!taggedObject.IsExplicit())
+                    throw new ArgumentException("object implicit - explicit expected.");
+
+                return GetInstance(taggedObject.GetObject());
+            }
+
             Asn1Object baseObject = taggedObject.GetObject();
 
-            if (declaredExplicit || baseObject is Asn1OctetString)
+            // If parsed as explicit though declared implicit, it should have been a set of one
+            if (taggedObject.IsExplicit())
             {
-                return GetInstance(baseObject);
+                Asn1OctetString singleSegment = GetInstance(baseObject);
+
+                if (taggedObject is BerTaggedObject)
+                    return new BerOctetString(new Asn1OctetString[]{ singleSegment });
+
+                return singleSegment;
             }
 
-            return BerOctetString.FromSequence(Asn1Sequence.GetInstance(baseObject));
+            if (baseObject is Asn1OctetString)
+                return (Asn1OctetString)baseObject;
+
+            // Parser assumes implicit constructed encodings are sequences
+            if (baseObject is Asn1Sequence)
+                return ((Asn1Sequence)baseObject).ToAsn1OctetString();
+
+            throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(taggedObject),
+                "taggedObject");
         }
 
         internal readonly byte[] contents;