summary refs log tree commit diff
path: root/crypto/src/asn1/DerUniversalString.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/DerUniversalString.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/DerUniversalString.cs')
-rw-r--r--crypto/src/asn1/DerUniversalString.cs157
1 files changed, 109 insertions, 48 deletions
diff --git a/crypto/src/asn1/DerUniversalString.cs b/crypto/src/asn1/DerUniversalString.cs
index 1ae989f93..e4e93bd7d 100644
--- a/crypto/src/asn1/DerUniversalString.cs
+++ b/crypto/src/asn1/DerUniversalString.cs
@@ -1,4 +1,6 @@
 using System;
+using System.Diagnostics;
+using System.IO;
 using System.Text;
 
 using Org.BouncyCastle.Utilities;
@@ -6,17 +8,27 @@ using Org.BouncyCastle.Utilities;
 namespace Org.BouncyCastle.Asn1
 {
     /**
-     * Der UniversalString object.
+     * UniversalString object.
      */
     public class DerUniversalString
         : DerStringBase
     {
-        private static readonly char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+        internal class Meta : Asn1UniversalType
+        {
+            internal static readonly Asn1UniversalType Instance = new Meta();
+
+            private Meta() : base(typeof(DerUniversalString), Asn1Tags.UniversalString) {}
+
+            internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
+            {
+                return CreatePrimitive(octetString.GetOctets());
+            }
+        }
 
-		private readonly byte[] str;
+        private static readonly char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
 
 		/**
-         * return a Universal string from the passed in object.
+         * return a universal string from the passed in object.
          *
          * @exception ArgumentException if the object cannot be converted.
          */
@@ -27,85 +39,134 @@ namespace Org.BouncyCastle.Asn1
             {
                 return (DerUniversalString)obj;
             }
+            else if (obj is IAsn1Convertible)
+            {
+                Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object();
+                if (asn1Object is DerUniversalString)
+                    return (DerUniversalString)asn1Object;
+            }
+            else if (obj is byte[])
+            {
+                try
+                {
+                    return (DerUniversalString)Meta.Instance.FromByteArray((byte[])obj);
+                }
+                catch (IOException e)
+                {
+                    throw new ArgumentException("failed to construct universal string from byte[]: " + e.Message);
+                }
+            }
 
             throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
         }
 
         /**
-         * return a Universal string from a tagged object.
+         * return a universal string from a tagged object.
          *
-         * @param obj the tagged object holding the object we want
-         * @param explicitly true if the object is meant to be explicitly
-         *              tagged false otherwise.
-         * @exception ArgumentException 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 ArgumentException if the tagged object cannot be converted.
          */
-        public static DerUniversalString GetInstance(
-            Asn1TaggedObject	obj,
-            bool				isExplicit)
+        public static DerUniversalString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-			Asn1Object o = obj.GetObject();
+            return (DerUniversalString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
+        }
 
-			if (isExplicit || o is DerUniversalString)
-			{
-				return GetInstance(o);
-			}
+        private readonly byte[] m_contents;
 
-			return new DerUniversalString(Asn1OctetString.GetInstance(o).GetOctets());
+        public DerUniversalString(byte[] contents)
+            : this(contents, true)
+        {
         }
 
-        /**
-         * basic constructor - byte encoded string.
-         */
-        public DerUniversalString(
-            byte[] str)
+        internal DerUniversalString(byte[] contents, bool clone)
         {
-			if (str == null)
-				throw new ArgumentNullException("str");
+            if (null == contents)
+                throw new ArgumentNullException("contents");
 
-			this.str = str;
+            m_contents = clone ? Arrays.Clone(contents) : contents;
         }
 
         public override string GetString()
         {
-			StringBuilder buffer = new StringBuilder("#");
-			byte[] enc = GetDerEncoded();
+            int dl = m_contents.Length;
+            int capacity = 3 + 2 * (Asn1OutputStream.GetLengthOfDL(dl) + dl);
+            StringBuilder buf = new StringBuilder("#1C", capacity);
+            EncodeHexDL(buf, dl);
 
-			for (int i = 0; i != enc.Length; i++)
-			{
-				uint ubyte = enc[i];
-				buffer.Append(table[(ubyte >> 4) & 0xf]);
-				buffer.Append(table[enc[i] & 0xf]);
-			}
+            for (int i = 0; i < dl; ++i)
+            {
+                EncodeHexByte(buf, m_contents[i]);
+            }
 
-            return buffer.ToString();
+            Debug.Assert(buf.Length == capacity);
+            return buf.ToString();
         }
 
-		public byte[] GetOctets()
+        public byte[] GetOctets()
         {
-            return (byte[]) str.Clone();
+            return Arrays.Clone(m_contents);
         }
 
         internal override IAsn1Encoding GetEncoding(int encoding)
         {
-            return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.UniversalString, this.str);
+            return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.UniversalString, m_contents);
         }
 
         internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
         {
-            return new PrimitiveEncoding(tagClass, tagNo, this.str);
+            return new PrimitiveEncoding(tagClass, tagNo, m_contents);
+        }
+
+        protected override bool Asn1Equals(Asn1Object asn1Object)
+        {
+            DerUniversalString that = asn1Object as DerUniversalString;
+            return null != that
+                && Arrays.AreEqual(this.m_contents, that.m_contents);
+        }
+
+        protected override int Asn1GetHashCode()
+        {
+            return Arrays.GetHashCode(m_contents);
+        }
+
+        internal static DerUniversalString CreatePrimitive(byte[] contents)
+        {
+            return new DerUniversalString(contents, false);
+        }
+
+        private static void EncodeHexByte(StringBuilder buf, int i)
+        {
+            buf.Append(table[(i >> 4) & 0xF]);
+            buf.Append(table[i & 0xF]);
         }
 
-        protected override bool Asn1Equals(
-			Asn1Object asn1Object)
-		{
-			DerUniversalString other = asn1Object as DerUniversalString;
+        private static void EncodeHexDL(StringBuilder buf, int dl)
+        {
+            if (dl < 128)
+            {
+                EncodeHexByte(buf, dl);
+                return;
+            }
+
+            byte[] stack = new byte[5];
+            int pos = 5;
 
-			if (other == null)
-				return false;
+            do
+            {
+                stack[--pos] = (byte)dl;
+                dl >>= 8;
+            }
+            while (dl != 0);
+
+            int count = stack.Length - pos;
+            stack[--pos] = (byte)(0x80 | count);
 
-//			return this.GetString().Equals(other.GetString());
-			return Arrays.AreEqual(this.str, other.str);
+            do
+            {
+                EncodeHexByte(buf, stack[pos++]);
+            }
+            while (pos < stack.Length);
         }
     }
 }