|
1 | | -/* |
| 1 | +/* |
2 | 2 | * Copyright © 2020-2024 Estonian Information System Authority |
3 | 3 | * |
4 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
22 | 22 | namespace WebEid.Security.Tests.Validator.Ocsp |
23 | 23 | { |
24 | 24 | using System; |
25 | | - using Exceptions; |
26 | 25 | using NUnit.Framework; |
| 26 | + using Org.BouncyCastle.Ocsp; |
27 | 27 | using Security.Validator.Ocsp; |
28 | | - using TestUtils; |
| 28 | + using WebEid.Security.Validator; |
| 29 | + using Org.BouncyCastle.Asn1; |
| 30 | + using Org.BouncyCastle.Asn1.Ocsp; |
| 31 | + using System.Globalization; |
| 32 | + using WebEid.Security.Exceptions; |
| 33 | + using WebEid.Security.Tests.TestUtils; |
| 34 | + using WebEid.Security.Util; |
| 35 | + using System.Runtime.CompilerServices; |
29 | 36 |
|
30 | 37 | [TestFixture] |
31 | 38 | public class OcspResponseValidatorTests |
32 | 39 | { |
| 40 | + private static TimeSpan timeSkew; |
| 41 | + private static TimeSpan maxThisUpdateAge; |
| 42 | + |
| 43 | + [SetUp] |
| 44 | + public void SetUp() |
| 45 | + { |
| 46 | + var configuration = new AuthTokenValidationConfiguration(); |
| 47 | + timeSkew = configuration.AllowedOcspResponseTimeSkew; |
| 48 | + maxThisUpdateAge = configuration.MaxOcspResponseThisUpdateAge; |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public void WhenThisAndNextUpdateWithinSkewThenValidationSucceeds() |
| 53 | + { |
| 54 | + var now = DateTimeProvider.UtcNow; |
| 55 | + var thisUpdateWithinAgeLimit = GetThisUpdateWithinAgeLimit(now); |
| 56 | + var nextUpdateWithinAgeLimit = now.Subtract(maxThisUpdateAge.Subtract(TimeSpan.FromSeconds(2))); |
| 57 | + |
| 58 | + var mockedResponse = new SingleResp(new SingleResponse(null, null, thisUpdateWithinAgeLimit.ToDerGenTime(), nextUpdateWithinAgeLimit.ToDerGenTime(), null)); |
| 59 | + |
| 60 | + Assert.DoesNotThrow(() => |
| 61 | + OcspResponseValidator.ValidateCertificateStatusUpdateTime(mockedResponse, timeSkew, maxThisUpdateAge)); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void WhenNextUpdateBeforeThisUpdateThenThrows() |
| 66 | + { |
| 67 | + var now = DateTimeProvider.UtcNow; |
| 68 | + var thisUpdateWithinAgeLimit = GetThisUpdateWithinAgeLimit(now); |
| 69 | + var beforeThisUpdate = thisUpdateWithinAgeLimit.Subtract(TimeSpan.FromSeconds(1)); |
| 70 | + |
| 71 | + var mockedResponse = new SingleResp(new SingleResponse(null, null, thisUpdateWithinAgeLimit.ToDerGenTime(), beforeThisUpdate.ToDerGenTime(), null)); |
| 72 | + |
| 73 | + Assert.Throws<UserCertificateOcspCheckFailedException>(() => |
| 74 | + OcspResponseValidator.ValidateCertificateStatusUpdateTime(mockedResponse, timeSkew, maxThisUpdateAge)) |
| 75 | + .HasMessageStartingWith("User certificate revocation check has failed: " |
| 76 | + + "Certificate status update time check failed: " |
| 77 | + + $"nextUpdate {beforeThisUpdate.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture)} is before thisUpdate {thisUpdateWithinAgeLimit.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture)}"); |
| 78 | + } |
| 79 | + |
33 | 80 | [Test] |
34 | | - public void WhenThisUpdateDayBeforeProducedAtThenThrows() |
| 81 | + public void WhenThisUpdateHalfHourBeforeNowThenThrows() |
35 | 82 | { |
36 | | - var thisUpdate = new DateTime(2021, 9, 1, 0, 0, 0, DateTimeKind.Utc); |
37 | | - var producedAt = new DateTime(2021, 9, 2, 0, 0, 0, DateTimeKind.Utc); |
| 83 | + var now = DateTimeProvider.UtcNow; |
| 84 | + var halfHourBeforeNow = now.Subtract(TimeSpan.FromMinutes(30)); |
| 85 | + var mockedResponse = new SingleResp(new SingleResponse(null, null, halfHourBeforeNow.ToDerGenTime(), null, null)); |
| 86 | + |
38 | 87 | Assert.Throws<UserCertificateOcspCheckFailedException>(() => |
39 | | - OcspResponseValidator.ValidateCertificateStatusUpdateTime(thisUpdate, null, producedAt)) |
40 | | - .WithMessage("User certificate revocation check has failed: " |
41 | | - + "Certificate status update time check failed: " |
42 | | - + "notAllowedBefore: 2021-09-01 23:45:00 +00:00, " |
43 | | - + "notAllowedAfter: 2021-09-02 00:15:00 +00:00, " |
44 | | - + "thisUpdate: 2021-09-01 00:00:00 +00:00, " |
45 | | - + "nextUpdate: null"); |
| 88 | + OcspResponseValidator.ValidateCertificateStatusUpdateTime(mockedResponse, timeSkew, maxThisUpdateAge)) |
| 89 | + .HasMessageStartingWith("User certificate revocation check has failed: " |
| 90 | + + "Certificate status update time check failed: " |
| 91 | + + $"thisUpdate {halfHourBeforeNow.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture)} is too old, minimum time allowed: "); |
46 | 92 | } |
| 93 | + |
47 | 94 | [Test] |
48 | | - public void WhenThisUpdateDayAfterProducedAtThenThrows() |
| 95 | + public void WhenThisUpdateHalfHourAfterNowThenThrows() |
49 | 96 | { |
50 | | - var thisUpdate = new DateTime(2021, 9, 2, 0, 0, 0, DateTimeKind.Utc); |
51 | | - var producedAt = new DateTime(2021, 9, 1, 0, 0, 0, DateTimeKind.Utc); |
| 97 | + var now = DateTimeProvider.UtcNow; |
| 98 | + var halfHourAfterNow = now.Add(TimeSpan.FromMinutes(30)); |
| 99 | + var mockedResponse = new SingleResp(new SingleResponse(null, null, halfHourAfterNow.ToDerGenTime(), null, null)); |
| 100 | + |
52 | 101 | Assert.Throws<UserCertificateOcspCheckFailedException>(() => |
53 | | - OcspResponseValidator.ValidateCertificateStatusUpdateTime(thisUpdate, null, producedAt)) |
54 | | - .WithMessage("User certificate revocation check has failed: " |
55 | | - + "Certificate status update time check failed: " |
56 | | - + "notAllowedBefore: 2021-08-31 23:45:00 +00:00, " |
57 | | - + "notAllowedAfter: 2021-09-01 00:15:00 +00:00, " |
58 | | - + "thisUpdate: 2021-09-02 00:00:00 +00:00, " |
59 | | - + "nextUpdate: null"); |
| 102 | + OcspResponseValidator.ValidateCertificateStatusUpdateTime(mockedResponse, timeSkew, maxThisUpdateAge)) |
| 103 | + .HasMessageStartingWith("User certificate revocation check has failed: " |
| 104 | + + "Certificate status update time check failed: " |
| 105 | + + $"thisUpdate {halfHourAfterNow.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture)} is too far in the future, latest allowed: "); |
60 | 106 | } |
61 | 107 |
|
62 | 108 | [Test] |
63 | | - public void WhenNextUpdateDayBeforeProducedAtThenThrows() |
| 109 | + public void WhenNextUpdateHalfHourBeforeNowThenThrows() |
64 | 110 | { |
65 | | - var thisUpdate = new DateTime(2021, 9, 2, 0, 0, 0, DateTimeKind.Utc); |
66 | | - var nextUpdate = new DateTime(2021, 9, 1, 0, 0, 0, DateTimeKind.Utc); |
67 | | - var producedAt = new DateTime(2021, 9, 1, 0, 0, 0, DateTimeKind.Utc); |
| 111 | + var now = DateTimeProvider.UtcNow; |
| 112 | + var thisUpdateWithinAgeLimit = GetThisUpdateWithinAgeLimit(now); |
| 113 | + var halfHourBeforeNow = now.Subtract(TimeSpan.FromMinutes(30)); |
| 114 | + var mockedResponse = new SingleResp(new SingleResponse(null, null, thisUpdateWithinAgeLimit.ToDerGenTime(), halfHourBeforeNow.ToDerGenTime(), null)); |
| 115 | + |
68 | 116 | Assert.Throws<UserCertificateOcspCheckFailedException>(() => |
69 | | - OcspResponseValidator.ValidateCertificateStatusUpdateTime(thisUpdate, nextUpdate, producedAt)) |
70 | | - .WithMessage("User certificate revocation check has failed: " |
71 | | - + "Certificate status update time check failed: " |
72 | | - + "notAllowedBefore: 2021-08-31 23:45:00 +00:00, " |
73 | | - + "notAllowedAfter: 2021-09-01 00:15:00 +00:00, " |
74 | | - + "thisUpdate: 2021-09-02 00:00:00 +00:00, " |
75 | | - + "nextUpdate: 2021-09-01 00:00:00 +00:00"); |
| 117 | + OcspResponseValidator.ValidateCertificateStatusUpdateTime(mockedResponse, timeSkew, maxThisUpdateAge)) |
| 118 | + .HasMessageStartingWith("User certificate revocation check has failed: " |
| 119 | + + "Certificate status update time check failed: " |
| 120 | + + $"nextUpdate {halfHourBeforeNow.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture)} is in the past"); |
| 121 | + } |
| 122 | + |
| 123 | + private static DateTime GetThisUpdateWithinAgeLimit(DateTime now) |
| 124 | + { |
| 125 | + var maxThisUpdateAgeMinusOne = maxThisUpdateAge.Subtract(TimeSpan.FromSeconds(1)); |
| 126 | + return now.Subtract(maxThisUpdateAgeMinusOne); |
76 | 127 | } |
77 | 128 | } |
78 | 129 | } |
0 commit comments