summary refs log tree commit diff
path: root/crypto/src/asn1/Asn1Null.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/Asn1Null.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/Asn1Null.cs')
-rw-r--r--crypto/src/asn1/Asn1Null.cs65
1 files changed, 61 insertions, 4 deletions
diff --git a/crypto/src/asn1/Asn1Null.cs b/crypto/src/asn1/Asn1Null.cs
index d54019f67..9ea9b4375 100644
--- a/crypto/src/asn1/Asn1Null.cs
+++ b/crypto/src/asn1/Asn1Null.cs
@@ -1,3 +1,8 @@
+using System;
+using System.IO;
+
+using Org.BouncyCastle.Utilities;
+
 namespace Org.BouncyCastle.Asn1
 {
     /**
@@ -6,13 +11,65 @@ namespace Org.BouncyCastle.Asn1
     public abstract class Asn1Null
         : Asn1Object
     {
+        internal class Meta : Asn1UniversalType
+        {
+            internal static readonly Asn1UniversalType Instance = new Meta();
+
+            private Meta() : base(typeof(Asn1Null), Asn1Tags.Null) {}
+
+            internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
+            {
+                return CreatePrimitive(octetString.GetOctets());
+            }
+        }
+
+        public static Asn1Null GetInstance(object obj)
+        {
+            if (obj == null || obj is Asn1Null)
+            {
+                return (Asn1Null)obj;
+            }
+            else if (obj is IAsn1Convertible)
+            {
+                Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object();
+                if (asn1Object is Asn1Null)
+                    return (Asn1Null)asn1Object;
+            }
+            else if (obj is byte[])
+            {
+                try
+                {
+                    return (Asn1Null)Meta.Instance.FromByteArray((byte[])obj);
+                }
+                catch (IOException e)
+                {
+                    throw new ArgumentException("failed to construct NULL from byte[]: " + e.Message);
+                }
+            }
+
+            throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
+        }
+
+        public static Asn1Null GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
+        {
+            return (Asn1Null)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
+        }
+
         internal Asn1Null()
         {
         }
 
-		public override string ToString()
-		{
-			return "NULL";
-		}
+        public override string ToString()
+        {
+            return "NULL";
+        }
+
+        internal static Asn1Null CreatePrimitive(byte[] contents)
+        {
+            if (0 != contents.Length)
+                throw new InvalidOperationException("malformed NULL encoding encountered");
+
+            return DerNull.Instance;
+        }
     }
 }