From ee804df4e9fce47ff92a39524646c4aba0a90ddc Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 27 Oct 2022 20:10:48 +0700 Subject: DateTimeUtilities improvements: - DateTimeToUnixMs converts to UTC - UnixMsToDateTime checks input range --- crypto/src/bcpg/SignaturePacket.cs | 10 +++--- crypto/src/security/JksStore.cs | 6 ++-- crypto/src/util/date/DateTimeUtilities.cs | 58 ++++++++++++++++++------------- 3 files changed, 41 insertions(+), 33 deletions(-) (limited to 'crypto/src') 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; - /// - /// Return the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC) for a given DateTime value. - /// - /// A UTC DateTime value not before epoch. - /// Number of whole milliseconds after epoch. - /// 'dateTime' is before epoch. - public static long DateTimeToUnixMs( - DateTime dateTime) + /// + /// Return the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC) for a given DateTime value. + /// + /// The DateTime value will be converted to UTC (using before + /// conversion. + /// A DateTime value not before the epoch. + /// Number of whole milliseconds after epoch. + /// 'dateTime' is before the epoch. + 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; } - /// - /// Create a DateTime value from the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC). - /// - /// Number of milliseconds since the epoch. - /// A UTC DateTime value - public static DateTime UnixMsToDateTime( - long unixMs) + /// + /// Create a UTC DateTime value from the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC). + /// + /// Number of milliseconds since the epoch. + /// A UTC DateTime value + /// 'unixMs' is before 'MinUnixMs' or after 'MaxUnixMs'. + /// + 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); } /// -- cgit 1.5.1