summary refs log tree commit diff
path: root/crypto/src/asn1/DerObjectIdentifier.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-18 13:46:18 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-18 13:46:18 +0700
commit4fccb8f490eefe7181558b2c3376ab23c33632ee (patch)
treefe8c1877bb5567cb4823823af77e5c8a38423db8 /crypto/src/asn1/DerObjectIdentifier.cs
parentASN.1: Staged encoding (diff)
downloadBouncyCastle.NET-ed25519-4fccb8f490eefe7181558b2c3376ab23c33632ee.tar.xz
ASN.1: Port of bc-java TYPE instances
- we use Meta.Instance here due to syntax restrictions
- also reworked some ASN.1 string types
Diffstat (limited to 'crypto/src/asn1/DerObjectIdentifier.cs')
-rw-r--r--crypto/src/asn1/DerObjectIdentifier.cs31
1 files changed, 24 insertions, 7 deletions
diff --git a/crypto/src/asn1/DerObjectIdentifier.cs b/crypto/src/asn1/DerObjectIdentifier.cs
index f91ad2150..8b77f966c 100644
--- a/crypto/src/asn1/DerObjectIdentifier.cs
+++ b/crypto/src/asn1/DerObjectIdentifier.cs
@@ -10,13 +10,25 @@ namespace Org.BouncyCastle.Asn1
     public class DerObjectIdentifier
         : Asn1Object
     {
+        internal class Meta : Asn1UniversalType
+        {
+            internal static readonly Asn1UniversalType Instance = new Meta();
+
+            private Meta() : base(typeof(DerObjectIdentifier), Asn1Tags.ObjectIdentifier) {}
+
+            internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
+            {
+                return CreatePrimitive(octetString.GetOctets(), false);
+            }
+        }
+
         public static DerObjectIdentifier FromContents(byte[] contents)
         {
             return CreatePrimitive(contents, true);
         }
 
         /**
-         * return an Oid from the passed in object
+         * return an OID from the passed in object
          *
          * @exception ArgumentException if the object cannot be converted.
          */
@@ -36,7 +48,7 @@ namespace Org.BouncyCastle.Asn1
             {
                 try
                 {
-                    return GetInstance(FromByteArray((byte[])obj));
+                    return (DerObjectIdentifier)Meta.Instance.FromByteArray((byte[])obj);
                 }
                 catch (IOException e)
                 {
@@ -49,14 +61,19 @@ namespace Org.BouncyCastle.Asn1
 
         public static DerObjectIdentifier GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-            Asn1Object baseObject = taggedObject.GetObject();
-
-            if (declaredExplicit || baseObject is DerObjectIdentifier)
+            /*
+             * TODO[asn1] This block here is for backward compatibility, but should eventually be removed.
+             * 
+             * - see https://github.com/bcgit/bc-java/issues/1015
+             */
+            if (!declaredExplicit && !taggedObject.IsParsed())
             {
-                return GetInstance(baseObject);
+                Asn1Object baseObject = taggedObject.GetObject();
+                if (!(baseObject is DerObjectIdentifier))
+                    return FromContents(Asn1OctetString.GetInstance(baseObject).GetOctets());
             }
 
-            return FromContents(Asn1OctetString.GetInstance(baseObject).GetOctets());
+            return (DerObjectIdentifier)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
         }
 
         private const long LongLimit = (Int64.MaxValue >> 7) - 0x7F;