summary refs log tree commit diff
path: root/crypto/src/asn1/DerGraphicString.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/DerGraphicString.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/DerGraphicString.cs')
-rw-r--r--crypto/src/asn1/DerGraphicString.cs47
1 files changed, 28 insertions, 19 deletions
diff --git a/crypto/src/asn1/DerGraphicString.cs b/crypto/src/asn1/DerGraphicString.cs
index 52ccb7e93..cb32d14eb 100644
--- a/crypto/src/asn1/DerGraphicString.cs
+++ b/crypto/src/asn1/DerGraphicString.cs
@@ -1,4 +1,5 @@
 using System;
+using System.IO;
 
 using Org.BouncyCastle.Utilities;
 
@@ -7,6 +8,18 @@ namespace Org.BouncyCastle.Asn1
     public class DerGraphicString
         : DerStringBase
     {
+        internal class Meta : Asn1UniversalType
+        {
+            internal static readonly Asn1UniversalType Instance = new Meta();
+
+            private Meta() : base(typeof(DerGraphicString), Asn1Tags.GraphicString) {}
+
+            internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
+            {
+                return CreatePrimitive(octetString.GetOctets());
+            }
+        }
+
         /**
          * return a Graphic String from the passed in object
          *
@@ -20,16 +33,21 @@ namespace Org.BouncyCastle.Asn1
             {
                 return (DerGraphicString)obj;
             }
-
-            if (obj is byte[])
+            else if (obj is IAsn1Convertible)
+            {
+                Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object();
+                if (asn1Object is DerGraphicString)
+                    return (DerGraphicString)asn1Object;
+            }
+            else if (obj is byte[])
             {
                 try
                 {
-                    return (DerGraphicString)FromByteArray((byte[])obj);
+                    return (DerGraphicString)Meta.Instance.FromByteArray((byte[])obj);
                 }
-                catch (Exception e)
+                catch (IOException e)
                 {
-                    throw new ArgumentException("encoding error in GetInstance: " + e.ToString(), "obj");
+                    throw new ArgumentException("failed to construct graphic string from byte[]: " + e.Message);
                 }
             }
 
@@ -39,23 +57,14 @@ namespace Org.BouncyCastle.Asn1
         /**
          * return a Graphic String from a tagged object.
          *
-         * @param obj the tagged object holding the object we want
-         * @param explicit true if the object is meant to be explicitly
-         *              tagged false otherwise.
-         * @exception IllegalArgumentException if the tagged object cannot
-         *               be converted.
+         * @param taggedObject the tagged object holding the object we want
+         * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise.
+         * @exception IllegalArgumentException if the tagged object cannot be converted.
          * @return a DerGraphicString instance, or null.
          */
-        public static DerGraphicString GetInstance(Asn1TaggedObject obj, bool isExplicit)
+        public static DerGraphicString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-			Asn1Object o = obj.GetObject();
-
-            if (isExplicit || o is DerGraphicString)
-			{
-				return GetInstance(o);
-			}
-
-            return new DerGraphicString(((Asn1OctetString)o).GetOctets());
+            return (DerGraphicString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
         }
 
         private readonly byte[] m_contents;