summary refs log tree commit diff
path: root/crypto/src/asn1/x509
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/x509')
-rw-r--r--crypto/src/asn1/x509/Time.cs84
1 files changed, 41 insertions, 43 deletions
diff --git a/crypto/src/asn1/x509/Time.cs b/crypto/src/asn1/x509/Time.cs
index 8260043aa..7f2d43315 100644
--- a/crypto/src/asn1/x509/Time.cs
+++ b/crypto/src/asn1/x509/Time.cs
@@ -8,24 +8,41 @@ namespace Org.BouncyCastle.Asn1.X509
     public class Time
         : Asn1Encodable, IAsn1Choice
     {
-        private readonly Asn1Object time;
+        public static Time GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is Time time)
+                return time;
+            if (obj is Asn1UtcTime utcTime)
+                return new Time(utcTime);
+            if (obj is Asn1GeneralizedTime generalizedTime)
+                return new Time(generalizedTime);
+
+            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), nameof(obj));
+        }
 
-        public static Time GetInstance(
-            Asn1TaggedObject	obj,
-            bool				explicitly)
+        public static Time GetInstance(Asn1TaggedObject	taggedObject, bool declaredExplicit)
         {
-            return GetInstance(obj.GetObject());
+            return GetInstance(taggedObject.GetObject());
         }
 
-        public Time(
-            Asn1Object time)
+        private readonly Asn1Object m_timeObject;
+
+        public Time(Asn1GeneralizedTime generalizedTime)
         {
-            if (time == null)
-                throw new ArgumentNullException("time");
-            if (!(time is Asn1UtcTime) && !(time is Asn1GeneralizedTime))
-                throw new ArgumentException("unknown object passed to Time");
+            this.m_timeObject = generalizedTime ?? throw new ArgumentNullException(nameof(generalizedTime));
+        }
 
-            this.time = time;
+        public Time(Asn1UtcTime utcTime)
+        {
+            if (utcTime == null)
+                throw new ArgumentNullException(nameof(utcTime));
+
+            // Validate utcTime is in the appropriate year range
+            utcTime.ToDateTime(2049);
+
+            this.m_timeObject = utcTime;
         }
 
         /**
@@ -35,40 +52,18 @@ namespace Org.BouncyCastle.Asn1.X509
          */
         public Time(DateTime date)
         {
-            DateTime d = date.ToUniversalTime();
+            DateTime utc = date.ToUniversalTime();
 
-            if (d.Year < 1950 || d.Year > 2049)
+            if (utc.Year < 1950 || utc.Year > 2049)
             {
-                time = new DerGeneralizedTime(d);
+                m_timeObject = new DerGeneralizedTime(utc);
             }
             else
             {
-                time = new DerUtcTime(d);
+                m_timeObject = new DerUtcTime(utc, 2049);
             }
         }
 
-        public static Time GetInstance(object obj)
-        {
-            if (obj == null)
-                return null;
-            if (obj is Time time)
-                return time;
-            if (obj is Asn1UtcTime utcTime)
-                return new Time(utcTime);
-            if (obj is Asn1GeneralizedTime generalizedTime)
-                return new Time(generalizedTime);
-
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
-        }
-
-        public string GetTime()
-        {
-            if (time is Asn1UtcTime utcTime)
-                return utcTime.AdjustedTimeString;
-
-            return ((Asn1GeneralizedTime)time).GetTime();
-        }
-
         /// <summary>
         /// Return our time as DateTime.
         /// </summary>
@@ -77,10 +72,10 @@ namespace Org.BouncyCastle.Asn1.X509
         {
             try
             {
-                if (time is Asn1UtcTime utcTime)
-                    return utcTime.ToAdjustedDateTime();
+                if (m_timeObject is Asn1UtcTime utcTime)
+                    return utcTime.ToDateTime(2049);
 
-                return ((Asn1GeneralizedTime)time).ToDateTime();
+                return ((Asn1GeneralizedTime)m_timeObject).ToDateTime();
             }
             catch (FormatException e)
             {
@@ -99,12 +94,15 @@ namespace Org.BouncyCastle.Asn1.X509
          */
         public override Asn1Object ToAsn1Object()
         {
-            return time;
+            return m_timeObject;
         }
 
         public override string ToString()
         {
-            return GetTime();
+            if (m_timeObject is Asn1UtcTime utcTime)
+                return utcTime.ToDateTime(2049).ToString(@"yyyyMMddHHmmssK", DateTimeFormatInfo.InvariantInfo);
+
+            return ((Asn1GeneralizedTime)m_timeObject).GetTime();
         }
     }
 }