summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-27 20:10:48 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-27 20:10:48 +0700
commitee804df4e9fce47ff92a39524646c4aba0a90ddc (patch)
treecd52aa3902db7502db774b9fb7fbe1d956d765df /crypto/src
parentRefactoring in Bcpg.Sig (diff)
downloadBouncyCastle.NET-ed25519-ee804df4e9fce47ff92a39524646c4aba0a90ddc.tar.xz
DateTimeUtilities improvements:
- DateTimeToUnixMs converts to UTC
- UnixMsToDateTime checks input range
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/bcpg/SignaturePacket.cs10
-rw-r--r--crypto/src/security/JksStore.cs6
-rw-r--r--crypto/src/util/date/DateTimeUtilities.cs58
3 files changed, 41 insertions, 33 deletions
diff --git a/crypto/src/bcpg/SignaturePacket.cs b/crypto/src/bcpg/SignaturePacket.cs
index 3256ee35e..dd9cc78e3 100644
--- a/crypto/src/bcpg/SignaturePacket.cs
+++ b/crypto/src/bcpg/SignaturePacket.cs
@@ -237,7 +237,7 @@ namespace Org.BouncyCastle.Bcpg
 
 			if (hashedData != null)
 			{
-				setCreationTime();
+				SetCreationTime();
 			}
 		}
 
@@ -446,18 +446,18 @@ namespace Org.BouncyCastle.Bcpg
 			return sOut.ToArray();
 		}
 
-		private void setCreationTime()
+		private void SetCreationTime()
 		{
 			foreach (SignatureSubpacket p in hashedData)
 			{
-				if (p is SignatureCreationTime)
+				if (p is SignatureCreationTime signatureCreationTime)
 				{
-                    creationTime = DateTimeUtilities.DateTimeToUnixMs(
-						((SignatureCreationTime)p).GetTime());
+                    creationTime = DateTimeUtilities.DateTimeToUnixMs(signatureCreationTime.GetTime());
 					break;
 				}
 			}
 		}
+
         public static SignaturePacket FromByteArray(byte[] data)
         {
             BcpgInputStream input = BcpgInputStream.Wrap(new MemoryStream(data));
diff --git a/crypto/src/security/JksStore.cs b/crypto/src/security/JksStore.cs
index 69ade11af..30b21fad2 100644
--- a/crypto/src/security/JksStore.cs
+++ b/crypto/src/security/JksStore.cs
@@ -461,8 +461,7 @@ namespace Org.BouncyCastle.Security
         private static DateTime ReadDateTime(BinaryReader br)
         {
             long unixMS = BinaryReaders.ReadInt64BigEndian(br);
-            DateTime unix = DateTimeUtilities.UnixMsToDateTime(unixMS);
-            return new DateTime(unix.Ticks, DateTimeKind.Utc);
+            return DateTimeUtilities.UnixMsToDateTime(unixMS);
         }
 
         private static X509Certificate ReadTypedCertificate(BinaryReader br, int storeVersion)
@@ -517,8 +516,7 @@ namespace Org.BouncyCastle.Security
 
         private static void WriteDateTime(BinaryWriter bw, DateTime dateTime)
         {
-            DateTime utc = dateTime.ToUniversalTime();
-            long unixMS = DateTimeUtilities.DateTimeToUnixMs(utc);
+            long unixMS = DateTimeUtilities.DateTimeToUnixMs(dateTime);
             BinaryWriters.WriteInt64BigEndian(bw, unixMS);
         }
 
diff --git a/crypto/src/util/date/DateTimeUtilities.cs b/crypto/src/util/date/DateTimeUtilities.cs
index 72e5123a2..3660e29c2 100644
--- a/crypto/src/util/date/DateTimeUtilities.cs
+++ b/crypto/src/util/date/DateTimeUtilities.cs
@@ -2,38 +2,48 @@ using System;
 
 namespace Org.BouncyCastle.Utilities.Date
 {
-	public class DateTimeUtilities
+	public static class DateTimeUtilities
 	{
-		public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static readonly DateTime UnixEpoch = DateTime.UnixEpoch;
+#else
+        public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+#endif
 
-		private DateTimeUtilities()
-		{
-		}
+        public static readonly long MaxUnixMs =
+            (DateTime.MaxValue.Ticks - UnixEpoch.Ticks) / TimeSpan.TicksPerMillisecond;
+        public static readonly long MinUnixMs = 0L;
 
-		/// <summary>
-		/// Return the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC) for a given DateTime value.
-		/// </summary>
-		/// <param name="dateTime">A UTC DateTime value not before epoch.</param>
-		/// <returns>Number of whole milliseconds after epoch.</returns>
-		/// <exception cref="ArgumentException">'dateTime' is before epoch.</exception>
-		public static long DateTimeToUnixMs(
-			DateTime dateTime)
+        /// <summary>
+        /// Return the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC) for a given DateTime value.
+        /// </summary>
+        /// <remarks>The DateTime value will be converted to UTC (using <see cref="DateTime.ToUniversalTime"/> before
+        /// conversion.</remarks>
+        /// <param name="dateTime">A DateTime value not before the epoch.</param>
+        /// <returns>Number of whole milliseconds after epoch.</returns>
+        /// <exception cref="ArgumentOutOfRangeException">'dateTime' is before the epoch.</exception>
+        public static long DateTimeToUnixMs(DateTime dateTime)
 		{
-			if (dateTime.CompareTo(UnixEpoch) < 0)
-				throw new ArgumentException("DateTime value may not be before the epoch", "dateTime");
+            DateTime utc = dateTime.ToUniversalTime();
+            if (utc.CompareTo(UnixEpoch) < 0)
+				throw new ArgumentOutOfRangeException(nameof(dateTime), "DateTime value may not be before the epoch");
 
-			return (dateTime.Ticks - UnixEpoch.Ticks) / TimeSpan.TicksPerMillisecond;
+			return (utc.Ticks - UnixEpoch.Ticks) / TimeSpan.TicksPerMillisecond;
 		}
 
-		/// <summary>
-		/// Create a DateTime value from the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC).
-		/// </summary>
-		/// <param name="unixMs">Number of milliseconds since the epoch.</param>
-		/// <returns>A UTC DateTime value</returns>
-		public static DateTime UnixMsToDateTime(
-			long unixMs)
+        /// <summary>
+        /// Create a UTC DateTime value from the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC).
+        /// </summary>
+        /// <param name="unixMs">Number of milliseconds since the epoch.</param>
+        /// <returns>A UTC DateTime value</returns>
+        /// <exception cref="ArgumentOutOfRangeException">'unixMs' is before 'MinUnixMs' or after 'MaxUnixMs'.
+        /// </exception>
+        public static DateTime UnixMsToDateTime(long unixMs)
 		{
-			return new DateTime(unixMs * TimeSpan.TicksPerMillisecond + UnixEpoch.Ticks, DateTimeKind.Utc);
+			if (unixMs < MinUnixMs || unixMs > MaxUnixMs)
+				throw new ArgumentOutOfRangeException(nameof(unixMs));
+
+            return new DateTime(unixMs * TimeSpan.TicksPerMillisecond + UnixEpoch.Ticks, DateTimeKind.Utc);
 		}
 
 		/// <summary>