Skip to content

Commit

Permalink
Pkcs9SigningTime: normalize exception on time < 1601
Browse files Browse the repository at this point in the history
Make Pkcs9SigningTime throw CryptographicException for all times which cannot be encoded.

In .NET Framework (and prior versions of .NET Core) an ArgumentException is thrown for values which cannot be expressed as a FILETIME value, and a CryptographicException for dates in [1601, 1949] and [2050, ...)

Commit migrated from dotnet/corefx@826f821
  • Loading branch information
MaximLipnin authored and bartonjs committed Jul 18, 2018
1 parent a4d8517 commit 0fc832c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ public sealed override byte[] DecodeOctetString(byte[] encodedOctets)

public sealed override byte[] EncodeUtcTime(DateTime utcTime)
{
long ft = utcTime.ToFileTimeUtc();
long ft;
try
{
ft = utcTime.ToFileTimeUtc();
}
catch (ArgumentException ex)
{
throw new CryptographicException(ex.Message, ex);
}

unsafe
{
return Interop.Crypt32.CryptEncodeObjectToByteArray(CryptDecodeObjectStructType.PKCS_UTC_TIME, &ft);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ public static void Pkcs9AttributeAsnEncodedDataCtorNullOid()
Assert.Throws<ArgumentNullException>(() => ign = new Pkcs9AttributeObject(a));
}

[Fact]
public static void InputDateTimeAsWindowsFileTimeBefore1601()
{
DateTime dt = new DateTime(1600, 12, 31, 11, 59, 59, DateTimeKind.Utc);
AssertExtensions.Throws<CryptographicException, ArgumentOutOfRangeException>(() => new Pkcs9SigningTime(dt));
}

[Fact]
public static void Pkcs9SigningTime_DateTimeMinValue()
{
AssertExtensions.Throws<CryptographicException, ArgumentOutOfRangeException>(() => new Pkcs9SigningTime(DateTime.MinValue));
}

[Fact]
public static void InputDateTimeAsX509TimeBefore1950_Utc()
{
Expand Down

0 comments on commit 0fc832c

Please sign in to comment.