summary refs log tree commit diff
path: root/crypto/src/asn1/DerUTCTime.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/DerUTCTime.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/DerUTCTime.cs')
-rw-r--r--crypto/src/asn1/DerUTCTime.cs95
1 files changed, 54 insertions, 41 deletions
diff --git a/crypto/src/asn1/DerUTCTime.cs b/crypto/src/asn1/DerUTCTime.cs
index eaa902e71..cb3f13353 100644
--- a/crypto/src/asn1/DerUTCTime.cs
+++ b/crypto/src/asn1/DerUTCTime.cs
@@ -1,6 +1,6 @@
 using System;
 using System.Globalization;
-using System.Text;
+using System.IO;
 
 using Org.BouncyCastle.Utilities;
 
@@ -12,47 +12,64 @@ namespace Org.BouncyCastle.Asn1
     public class DerUtcTime
         : Asn1Object
     {
-        private readonly string time;
+        internal class Meta : Asn1UniversalType
+        {
+            internal static readonly Asn1UniversalType Instance = new Meta();
+
+            private Meta() : base(typeof(DerUtcTime), Asn1Tags.UtcTime) {}
+
+            internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString)
+            {
+                return CreatePrimitive(octetString.GetOctets());
+            }
+        }
 
 		/**
-         * return an UTC Time from the passed in object.
+         * return a UTC Time from the passed in object.
          *
          * @exception ArgumentException if the object cannot be converted.
          */
-        public static DerUtcTime GetInstance(
-            object obj)
+        public static DerUtcTime GetInstance(object obj)
         {
             if (obj == null || obj is DerUtcTime)
             {
                 return (DerUtcTime)obj;
             }
+            else if (obj is IAsn1Convertible)
+            {
+                Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object();
+                if (asn1Object is DerUtcTime)
+                    return (DerUtcTime)asn1Object;
+            }
+            else if (obj is byte[])
+            {
+                try
+                {
+                    return (DerUtcTime)Meta.Instance.FromByteArray((byte[])obj);
+                }
+                catch (IOException e)
+                {
+                    throw new ArgumentException("failed to construct UTC time from byte[]: " + e.Message);
+                }
+            }
 
             throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
         }
 
         /**
-         * return an UTC Time from a tagged object.
+         * return a UTC Time 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 DerUtcTime GetInstance(
-            Asn1TaggedObject	obj,
-            bool				isExplicit)
+        public static DerUtcTime GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-			Asn1Object o = obj.GetObject();
-
-			if (isExplicit || o is DerUtcTime)
-			{
-				return GetInstance(o);
-			}
-
-			return new DerUtcTime(((Asn1OctetString)o).GetOctets());
+            return (DerUtcTime)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
         }
 
+        private readonly string time;
+
         /**
          * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
          * never encoded. When you're creating one of these objects from scratch, that's
@@ -62,8 +79,7 @@ namespace Org.BouncyCastle.Asn1
          * <p>
          * @param time the time string.</p>
          */
-        public DerUtcTime(
-            string time)
+        public DerUtcTime(string time)
         {
 			if (time == null)
 				throw new ArgumentNullException("time");
@@ -83,8 +99,7 @@ namespace Org.BouncyCastle.Asn1
 		/**
          * base constructor from a DateTime object
          */
-        public DerUtcTime(
-            DateTime time)
+        public DerUtcTime(DateTime time)
         {
 #if PORTABLE
             this.time = time.ToUniversalTime().ToString("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
@@ -93,13 +108,12 @@ namespace Org.BouncyCastle.Asn1
 #endif
         }
 
-		internal DerUtcTime(
-            byte[] bytes)
+		internal DerUtcTime(byte[] contents)
         {
             //
             // explicitly convert to characters
             //
-            this.time = Strings.FromAsciiByteArray(bytes);
+            this.time = Strings.FromAsciiByteArray(contents);
         }
 
 //		public DateTime ToDateTime()
@@ -139,9 +153,7 @@ namespace Org.BouncyCastle.Asn1
 			return ParseDateString(AdjustedTimeString, @"yyyyMMddHHmmss'GMT'zzz");
 		}
 
-		private DateTime ParseDateString(
-			string	dateStr,
-			string	formatStr)
+		private DateTime ParseDateString(string dateStr, string formatStr)
 		{
 			DateTime dt = DateTime.ParseExact(
 				dateStr,
@@ -247,15 +259,11 @@ namespace Org.BouncyCastle.Asn1
             return new PrimitiveEncoding(tagClass, tagNo, GetOctets());
         }
 
-        protected override bool Asn1Equals(
-			Asn1Object asn1Object)
+        protected override bool Asn1Equals(Asn1Object asn1Object)
 		{
-			DerUtcTime other = asn1Object as DerUtcTime;
-
-			if (other == null)
-				return false;
-
-			return this.time.Equals(other.time);
+			DerUtcTime that = asn1Object as DerUtcTime;
+            return null != that
+                && this.time.Equals(that.time);
         }
 
 		protected override int Asn1GetHashCode()
@@ -267,5 +275,10 @@ namespace Org.BouncyCastle.Asn1
 		{
 			return time;
 		}
-	}
+
+        internal static DerUtcTime CreatePrimitive(byte[] contents)
+        {
+            return new DerUtcTime(contents);
+        }
+    }
 }