summary refs log tree commit diff
path: root/crypto/src/asn1/DerInteger.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/DerInteger.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/DerInteger.cs')
-rw-r--r--crypto/src/asn1/DerInteger.cs62
1 files changed, 41 insertions, 21 deletions
diff --git a/crypto/src/asn1/DerInteger.cs b/crypto/src/asn1/DerInteger.cs
index 8b112f693..c8d4e47df 100644
--- a/crypto/src/asn1/DerInteger.cs
+++ b/crypto/src/asn1/DerInteger.cs
@@ -1,4 +1,5 @@
 using System;
+using System.IO;
 
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Utilities;
@@ -8,6 +9,18 @@ namespace Org.BouncyCastle.Asn1
     public class DerInteger
         : Asn1Object
     {
+        internal class Meta : Asn1UniversalType
+        {
+            internal static readonly Asn1UniversalType Instance = new Meta();
+
+            private Meta() : base(typeof(DerInteger), Asn1Tags.Integer) {}
+
+            internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
+            {
+                return CreatePrimitive(octetString.GetOctets());
+            }
+        }
+
         public const string AllowUnsafeProperty = "Org.BouncyCastle.Asn1.AllowUnsafeInteger";
 
         internal static bool AllowUnsafe()
@@ -27,13 +40,29 @@ namespace Org.BouncyCastle.Asn1
          *
          * @exception ArgumentException if the object cannot be converted.
          */
-        public static DerInteger GetInstance(
-            object obj)
+        public static DerInteger GetInstance(object obj)
         {
             if (obj == null || obj is DerInteger)
             {
                 return (DerInteger)obj;
             }
+            else if (obj is IAsn1Convertible)
+            {
+                Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object();
+                if (asn1Object is DerInteger)
+                    return (DerInteger)asn1Object;
+            }
+            else if (obj is byte[])
+            {
+                try
+                {
+                    return (DerInteger)Meta.Instance.FromByteArray((byte[])obj);
+                }
+                catch (IOException e)
+                {
+                    throw new ArgumentException("failed to construct integer from byte[]: " + e.Message);
+                }
+            }
 
             throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
         }
@@ -41,27 +70,13 @@ namespace Org.BouncyCastle.Asn1
         /**
          * return an Integer from a tagged object.
          *
-         * @param obj the tagged object holding the object we want
-         * @param isExplicit 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 DerInteger GetInstance(
-            Asn1TaggedObject	obj,
-            bool				isExplicit)
+        public static DerInteger GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-            if (obj == null)
-                throw new ArgumentNullException("obj");
-
-			Asn1Object o = obj.GetObject();
-
-			if (isExplicit || o is DerInteger)
-			{
-				return GetInstance(o);
-			}
-
-			return new DerInteger(Asn1OctetString.GetInstance(o).GetOctets());
+            return (DerInteger)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
         }
 
 		public DerInteger(int value)
@@ -198,6 +213,11 @@ namespace Org.BouncyCastle.Asn1
 			return Value.ToString();
 		}
 
+        internal static DerInteger CreatePrimitive(byte[] contents)
+        {
+            return new DerInteger(contents, false);
+        }
+
         internal static int IntValue(byte[] bytes, int start, int signExt)
         {
             int length = bytes.Length;