public override byte[] EncodeUtcTime(DateTime utcTime)
{
+ const int minLegalYear = 1950;
// Write using DER to support the most readers.
using (AsnWriter writer = new AsnWriter(AsnEncodingRules.DER))
{
- // Sending the DateTime through ToLocalTime here will cause the right normalization
- // of DateTimeKind.Unknown.
- //
- // Unknown => Local (adjust) => UTC (adjust "back", add Z marker; matches Windows)
- if (utcTime.Kind == DateTimeKind.Unspecified)
+ try
{
- writer.WriteUtcTime(utcTime.ToLocalTime());
+ // Sending the DateTime through ToLocalTime here will cause the right normalization
+ // of DateTimeKind.Unknown.
+ //
+ // Unknown => Local (adjust) => UTC (adjust "back", add Z marker; matches Windows)
+ if (utcTime.Kind == DateTimeKind.Unspecified)
+ {
+ writer.WriteUtcTime(utcTime.ToLocalTime(), minLegalYear);
+ }
+ else
+ {
+ writer.WriteUtcTime(utcTime, minLegalYear);
+ }
+
+ return writer.Encode();
}
- else
+ catch (ArgumentException ex)
{
- writer.WriteUtcTime(utcTime);
+ throw new CryptographicException(ex.Message, ex);
}
-
- return writer.Encode();
}
}
Assert.Throws<ArgumentNullException>(() => ign = new Pkcs9AttributeObject(a));
}
+ [Fact]
+ public static void InputDateTimeAsX509TimeBefore1950_Utc()
+ {
+ DateTime dt = new DateTime(1949, 12, 31, 23, 59, 59, DateTimeKind.Utc);
+ Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
+ }
+
+ [Fact]
+ public static void InputDateTimeAsX509TimeBefore1950_Unspecified()
+ {
+ DateTime dt = new DateTime(1949, 12, 30);
+ Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
+ }
+
+ [Fact]
+ public static void InputDateTimeAsX509TimeBefore1950_Local()
+ {
+ DateTime dt = new DateTime(1949, 12, 30, 00, 00, 00, DateTimeKind.Local);
+ Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
+ }
+
+ [Fact]
+ public static void InputDateTimeAsX509TimeAfter2049_Utc()
+ {
+ DateTime dt = new DateTime(2050, 01, 01, 00, 00, 00, DateTimeKind.Utc);
+ Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
+ }
+
+ [Fact]
+ public static void InputDateTimeAsX509TimeAfter2049_Unspecified()
+ {
+ DateTime dt = new DateTime(2050, 01, 02);
+ Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
+ }
+
+ [Fact]
+ public static void InputDateTimeAsX509TimeAfter2049_Local()
+ {
+ DateTime dt = new DateTime(2050, 01, 02, 00, 00, 00, DateTimeKind.Local);
+ Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
+ }
+
+ [Fact]
+ public static void InputDateTimeAsX509TimeBetween1950And2049_Utc()
+ {
+ var exception = Record.Exception(() => {
+ DateTime dt = new DateTime(1950, 1, 1, 00, 00, 00, DateTimeKind.Utc);
+ Pkcs9SigningTime st = new Pkcs9SigningTime(dt);
+ dt = new DateTime(2049, 12, 31, 23, 59, 59, DateTimeKind.Utc);
+ st = new Pkcs9SigningTime(dt);
+
+ dt = new DateTime(1950, 1, 2);
+ st = new Pkcs9SigningTime(dt);
+ dt = new DateTime(2049, 12, 30);
+ st = new Pkcs9SigningTime(dt);
+
+ dt = new DateTime(1950, 1, 2, 00, 00, 00, DateTimeKind.Local);
+ st = new Pkcs9SigningTime(dt);
+ dt = new DateTime(2049, 12, 30, 23, 59, 59, DateTimeKind.Local);
+ st = new Pkcs9SigningTime(dt);
+ });
+ Assert.Null(exception);
+ }
[Fact]
public static void Pkcs9AttributeAsnEncodedDataCtorNullOidValue()
Assert.Equal(dateTime, cookedData.ToLocalTime());
Assert.Equal(DateTimeKind.Utc, cookedData.Kind);
}
-
+
[Fact]
public static void SigningTimeFromCookedData_Utc()
{