Skip to content

Commit f3853b5

Browse files
committed
refactor: Extract FallbackOcspService interface, separate ResilientOcspServiceProviderTest from OcspServiceProviderTest
WE2-1178 Signed-off-by: Sven Mitt <svenzik@users.noreply.github.com>
1 parent c9fd42b commit f3853b5

11 files changed

Lines changed: 209 additions & 143 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package eu.webeid.ocsp.service;
2+
3+
import java.time.Duration;
4+
5+
public interface FallbackOcspService extends OcspService {
6+
Duration getMaxThisUpdateAge();
7+
8+
Duration getMaxNextUpdateAge();
9+
10+
FallbackOcspService getNextFallback();
11+
}

‎web-eid-ocsp/pom.xml‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@
1010
</parent>
1111

1212
<artifactId>web-eid-ocsp</artifactId>
13-
<version>1.0.0-SNAPSHOT</version>
14-
15-
<properties>
16-
<maven.compiler.source>17</maven.compiler.source>
17-
<maven.compiler.target>17</maven.compiler.target>
18-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19-
</properties>
2013

2114
<dependencies>
2215
<dependency>
@@ -34,4 +27,22 @@
3427
</dependency>
3528
</dependencies>
3629

30+
<build>
31+
<plugins>
32+
<!-- Generate test jar, so test helpers and resources are shared with other modules -->
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-jar-plugin</artifactId>
36+
<version>3.5.0</version>
37+
<executions>
38+
<execution>
39+
<goals>
40+
<goal>test-jar</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
3748
</project>

‎web-eid-ocsp/src/test/java/eu/webeid/ocsp/OcspCertificateRevocationCheckerNetworkTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import eu.webeid.ocsp.exceptions.UserCertificateOCSPCheckFailedException;
99
import eu.webeid.ocsp.service.AiaOcspServiceConfiguration;
1010
import eu.webeid.ocsp.service.DesignatedOcspServiceConfiguration;
11-
import eu.webeid.ocsp.service.OcspServiceProvider;
11+
import eu.webeid.ocsp.service.OcspServiceProviderImpl;
1212
import eu.webeid.security.certificate.CertificateValidator;
1313
import eu.webeid.security.exceptions.CertificateRevocationCheckFailedException;
1414
import eu.webeid.security.exceptions.CertificateRevokedException;
@@ -256,7 +256,7 @@ private OcspCertificateRevocationChecker customChecker(DesignatedOcspServiceConf
256256
List<X509Certificate> intermediates) throws Exception {
257257
return new OcspCertificateRevocationChecker(
258258
OcspClientImpl.build(Duration.ofSeconds(2)),
259-
new OcspServiceProvider(designated, new AiaOcspServiceConfiguration(Set.of(),
259+
new OcspServiceProviderImpl(designated, new AiaOcspServiceConfiguration(Set.of(),
260260
CertificateValidator.buildTrustAnchorsFromCertificates(anchors),
261261
CertificateValidator.buildCertStoreFromCertificates(intermediates),
262262
OcspCertificateRevocationChecker.DEFAULT_THIS_UPDATE_AGE, OcspCertificateRevocationChecker.DEFAULT_NEXT_UPDATE_AGE, true)),

‎web-eid-ocsp/src/test/java/eu/webeid/ocsp/service/OcspServiceMaker.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,27 @@ public class OcspServiceMaker {
4040
}
4141

4242
public static OcspServiceProvider getAiaOcspServiceProvider() throws JceException {
43-
return new OcspServiceProvider(null, getAiaOcspServiceConfiguration());
43+
return new OcspServiceProviderImpl(null, getAiaOcspServiceConfiguration());
4444
}
4545

4646
public static OcspServiceProvider getAiaOcspServiceProviderWithEsteid2018NonceDisabled() throws JceException {
47-
return new OcspServiceProvider(null, getAiaOcspServiceConfiguration(false));
47+
return new OcspServiceProviderImpl(null, getAiaOcspServiceConfiguration(false));
4848
}
4949

5050
public static OcspServiceProvider getDesignatedOcspServiceProvider() throws CertificateException, IOException, OCSPCertificateException, JceException {
51-
return new OcspServiceProvider(getDesignatedOcspServiceConfiguration(), getAiaOcspServiceConfiguration());
51+
return new OcspServiceProviderImpl(getDesignatedOcspServiceConfiguration(), getAiaOcspServiceConfiguration());
5252
}
5353

5454
public static OcspServiceProvider getDesignatedOcspServiceProvider(boolean doesSupportNonce) throws CertificateException, IOException, JceException, OCSPCertificateException {
55-
return new OcspServiceProvider(getDesignatedOcspServiceConfiguration(doesSupportNonce), getAiaOcspServiceConfiguration());
55+
return new OcspServiceProviderImpl(getDesignatedOcspServiceConfiguration(doesSupportNonce), getAiaOcspServiceConfiguration());
5656
}
5757

5858
public static OcspServiceProvider getDesignatedOcspServiceProvider(String ocspServiceAccessLocation) throws CertificateException, IOException, OCSPCertificateException, JceException {
5959
return getDesignatedOcspServiceProvider(true, ocspServiceAccessLocation);
6060
}
6161

6262
public static OcspServiceProvider getDesignatedOcspServiceProvider(boolean doesSupportNonce, String ocspServiceAccessLocation) throws CertificateException, IOException, OCSPCertificateException, JceException {
63-
return new OcspServiceProvider(getDesignatedOcspServiceConfiguration(doesSupportNonce, ocspServiceAccessLocation), getAiaOcspServiceConfiguration());
63+
return new OcspServiceProviderImpl(getDesignatedOcspServiceConfiguration(doesSupportNonce, ocspServiceAccessLocation), getAiaOcspServiceConfiguration());
6464
}
6565

6666
private static AiaOcspServiceConfiguration getAiaOcspServiceConfiguration() throws JceException {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// SPDX-FileCopyrightText: Estonian Information System Authority
2+
// SPDX-License-Identifier: MIT
3+
4+
package eu.webeid.ocsp.service;
5+
6+
import eu.webeid.ocsp.OcspCertificateRevocationChecker;
7+
import eu.webeid.ocsp.exceptions.OCSPCertificateException;
8+
import eu.webeid.security.certificate.CertificateValidator;
9+
import org.bouncycastle.cert.X509CertificateHolder;
10+
import org.junit.jupiter.api.Test;
11+
import eu.webeid.security.testutil.LocalOcspResponder;
12+
13+
import java.net.URI;
14+
import java.time.Duration;
15+
import java.util.Date;
16+
import java.util.List;
17+
import java.util.Set;
18+
19+
import static eu.webeid.ocsp.service.OcspServiceMaker.getAiaOcspServiceProvider;
20+
import static eu.webeid.ocsp.service.OcspServiceMaker.getDesignatedOcspServiceProvider;
21+
import static eu.webeid.security.testutil.Certificates.getDemoEsteidSk2018AiaOcspResponder;
22+
import static eu.webeid.security.testutil.Certificates.getJaakKristjanEsteid2018Cert;
23+
import static eu.webeid.security.testutil.Certificates.getTestEsteid2018CA;
24+
import static eu.webeid.security.testutil.Certificates.getMariliisEsteid2015Cert;
25+
import static eu.webeid.security.testutil.Certificates.getTestEsteid2015CA;
26+
import static eu.webeid.security.testutil.Certificates.getTestSelfSignedOcspResponder;
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.assertThatCode;
29+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
30+
31+
class OcspServiceProviderTest {
32+
33+
@Test
34+
void whenDesignatedOcspServiceConfigurationProvided_thenCreatesDesignatedOcspService() throws Exception {
35+
final OcspServiceProvider ocspServiceProvider = getDesignatedOcspServiceProvider();
36+
final OcspService service = ocspServiceProvider.getService(getJaakKristjanEsteid2018Cert(), getTestEsteid2018CA());
37+
assertThat(service.getAccessLocation()).isEqualTo(new URI("http://demo.sk.ee/ocsp"));
38+
assertThat(service.doesSupportNonce()).isTrue();
39+
assertThat(service.getMaxThisUpdateAge()).isEqualTo(Duration.ofMinutes(3));
40+
assertThat(service.getMaxNextUpdateAge()).isEqualTo(Duration.ofMinutes(20));
41+
assertThatCode(() ->
42+
service.validateResponderCertificate(new X509CertificateHolder(getTestSelfSignedOcspResponder().getEncoded()), getTestEsteid2018CA(), new Date(1630000000000L)))
43+
.doesNotThrowAnyException();
44+
assertThatCode(() ->
45+
service.validateResponderCertificate(new X509CertificateHolder(getTestEsteid2018CA().getEncoded()), getTestEsteid2018CA(), new Date(1630000000000L)))
46+
.isInstanceOf(OCSPCertificateException.class)
47+
.hasMessage("Responder certificate from the OCSP response is not equal to the configured designated OCSP responder certificate");
48+
}
49+
50+
@Test
51+
void whenAiaOcspServiceConfigurationProvided_thenCreatesAiaOcspService() throws Exception {
52+
final OcspServiceProvider ocspServiceProvider = getAiaOcspServiceProvider();
53+
final OcspService service2018 = ocspServiceProvider.getService(getJaakKristjanEsteid2018Cert(), getTestEsteid2018CA());
54+
assertThat(service2018.getAccessLocation()).isEqualTo(new URI("http://aia.demo.sk.ee/esteid2018"));
55+
assertThat(service2018.doesSupportNonce()).isTrue();
56+
57+
final OcspService service2015 = ocspServiceProvider.getService(getMariliisEsteid2015Cert(), getTestEsteid2015CA());
58+
assertThat(service2015.getAccessLocation()).isEqualTo(new URI("http://aia.demo.sk.ee/esteid2015"));
59+
assertThat(service2015.doesSupportNonce()).isFalse();
60+
assertThat(service2018.getMaxThisUpdateAge()).isEqualTo(Duration.ofMinutes(3));
61+
assertThat(service2018.getMaxNextUpdateAge()).isEqualTo(Duration.ofMinutes(20));
62+
assertThatCode(() ->
63+
service2018.validateResponderCertificate(new X509CertificateHolder(getDemoEsteidSk2018AiaOcspResponder().getEncoded()), getTestEsteid2018CA(), new Date(1630000000000L)))
64+
.doesNotThrowAnyException();
65+
}
66+
67+
@Test
68+
void whenAiaResponderCertificateLacksOcspSigningUsage_thenThrows() throws Exception {
69+
final OcspServiceProvider ocspServiceProvider = getAiaOcspServiceProvider();
70+
final OcspService service2018 = ocspServiceProvider.getService(getJaakKristjanEsteid2018Cert(), getTestEsteid2018CA());
71+
final X509CertificateHolder wrongResponderCert = new X509CertificateHolder(getMariliisEsteid2015Cert().getEncoded());
72+
assertThatExceptionOfType(OCSPCertificateException.class)
73+
.isThrownBy(() ->
74+
service2018.validateResponderCertificate(wrongResponderCert, getTestEsteid2018CA(), new Date(1630000000000L)))
75+
.withMessageContaining("does not contain the Extended Key Usage extension required for OCSP response signing");
76+
}
77+
78+
@Test
79+
void whenDifferentIssuersHaveSameName_thenDesignatedServiceAppliesOnlyToConfiguredCertificate() throws Exception {
80+
try (LocalOcspResponder first = new LocalOcspResponder();
81+
LocalOcspResponder second = new LocalOcspResponder()) {
82+
first.start();
83+
second.start();
84+
final var authorities = List.of(first.issuer(), second.issuer());
85+
final var designated = new DesignatedOcspServiceConfiguration(
86+
first.designatedUri(), first.responderCertificate(), List.of(first.issuer()), true,
87+
OcspCertificateRevocationChecker.DEFAULT_THIS_UPDATE_AGE, OcspCertificateRevocationChecker.DEFAULT_NEXT_UPDATE_AGE, true);
88+
final var aia = new AiaOcspServiceConfiguration(Set.of(),
89+
CertificateValidator.buildTrustAnchorsFromCertificates(authorities),
90+
CertificateValidator.buildCertStoreFromCertificates(authorities),
91+
OcspCertificateRevocationChecker.DEFAULT_THIS_UPDATE_AGE, OcspCertificateRevocationChecker.DEFAULT_NEXT_UPDATE_AGE, true);
92+
final var provider = new OcspServiceProviderImpl(designated, aia);
93+
94+
assertThat(first.issuer().getSubjectX500Principal()).isEqualTo(second.issuer().getSubjectX500Principal());
95+
assertThat(first.issuer()).isNotEqualTo(second.issuer());
96+
assertThat(provider.getService(first.subject(), first.issuer())).isInstanceOf(DesignatedOcspService.class);
97+
assertThat(provider.getService(second.subject(), second.issuer())).isInstanceOf(AiaOcspService.class);
98+
}
99+
}
100+
101+
}
102+
103+
// Old disabled example AuthTokenValidator test with designated OCSP check.
104+
//
105+
// @Test
106+
// @Disabled("A new designated test OCSP responder certificate was issued whose validity period no longer overlaps with the revoked certificate")
107+
// void whenCertificateIsRevoked_thenOcspCheckWithDesignatedOcspServiceFails() throws Exception {
108+
// mockDate("2020-01-01", mockedClock);
109+
// final AuthTokenValidator validatorWithOcspCheck = AuthTokenValidators.getAuthTokenValidatorWithDesignatedOcspCheck();
110+
// final WebEidAuthToken token = replaceTokenField(AUTH_TOKEN, "X5C", REVOKED_CERT);
111+
// assertThatThrownBy(() -> validatorWithOcspCheck
112+
// .validate(token, VALID_CHALLENGE_NONCE))
113+
// .isInstanceOf(UserCertificateRevokedException.class);
114+
// }

‎web-eid-resilient-ocsp/pom.xml‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@
1010
</parent>
1111

1212
<artifactId>web-eid-resilient-ocsp</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
15-
<properties>
16-
<maven.compiler.source>17</maven.compiler.source>
17-
<maven.compiler.target>17</maven.compiler.target>
18-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19-
</properties>
2013

2114
<dependencies>
2215
<dependency>
2316
<groupId>eu.webeid.security</groupId>
2417
<artifactId>web-eid-ocsp</artifactId>
25-
<version>1.0.0-SNAPSHOT</version>
18+
<version>${project.parent.version}</version>
2619
</dependency>
2720
<dependency>
2821
<groupId>io.github.resilience4j</groupId>
@@ -52,13 +45,21 @@
5245
<artifactId>resilience4j-vavr</artifactId>
5346
<version>${resilience4j.version}</version>
5447
</dependency>
48+
5549
<dependency>
5650
<groupId>eu.webeid.security</groupId>
5751
<artifactId>authtoken-validation</artifactId>
5852
<version>${project.parent.version}</version>
5953
<classifier>tests</classifier>
6054
<scope>test</scope>
6155
</dependency>
56+
<dependency>
57+
<groupId>eu.webeid.security</groupId>
58+
<artifactId>web-eid-ocsp</artifactId>
59+
<version>${project.parent.version}</version>
60+
<classifier>tests</classifier>
61+
<scope>test</scope>
62+
</dependency>
6263
</dependencies>
6364

6465
</project>

web-eid-resilient-ocsp/src/main/java/eu/webeid/ocsp/service/FallbackOcspService.java renamed to web-eid-resilient-ocsp/src/main/java/eu/webeid/ocsp/service/ResilientFallbackOcspService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@
2424

2525
import static eu.webeid.security.certificate.CertificateValidator.requireCertificateIsValidOnDate;
2626

27-
public class FallbackOcspService implements OcspService {
27+
public class ResilientFallbackOcspService implements FallbackOcspService {
2828

2929
private final JcaX509CertificateConverter certificateConverter = new JcaX509CertificateConverter();
3030
private final URI url;
3131
private final boolean supportsNonce;
3232
private final X509Certificate trustedResponderCertificate;
3333
private final X509Certificate issuerCertificate;
34-
private final FallbackOcspService nextFallback;
34+
private final ResilientFallbackOcspService nextFallback;
3535
private final Set<TrustAnchor> trustedCACertificateAnchors;
3636
private final CertStore trustedCACertificateCertStore;
3737
private final Duration maxThisUpdateAge;
3838
private final Duration maxNextUpdateAge;
3939
private final boolean isKeyUsageRequired;
4040

41-
public FallbackOcspService(FallbackOcspServiceConfiguration configuration) {
41+
public ResilientFallbackOcspService(FallbackOcspServiceConfiguration configuration) {
4242
this.url = configuration.getAccessLocation();
4343
this.supportsNonce = configuration.doesSupportNonce();
4444
this.trustedResponderCertificate = configuration.getResponderCertificate();
4545
this.issuerCertificate = configuration.getIssuerCertificate();
4646
this.nextFallback = configuration.getNextFallbackConfiguration() != null
47-
? new FallbackOcspService(configuration.getNextFallbackConfiguration())
47+
? new ResilientFallbackOcspService(configuration.getNextFallbackConfiguration())
4848
: null;
4949
this.trustedCACertificateAnchors = configuration.getTrustedCACertificateAnchors();
5050
this.trustedCACertificateCertStore = configuration.getTrustedCACertificateCertStore();
@@ -127,7 +127,8 @@ private void validateResponderCertificateAgainstTrustedCa(X509Certificate respon
127127
);
128128
}
129129

130-
public FallbackOcspService getNextFallback() {
130+
@Override
131+
public ResilientFallbackOcspService getNextFallback() {
131132
return nextFallback;
132133
}
133134
}

‎web-eid-resilient-ocsp/src/test/java/eu/webeid/ocsp/service/AiaOcspServiceTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/**
3030
* Unit tests for the {@link AiaOcspService} behaviour introduced in commit e2bd57e3:
3131
* nonce support is now keyed on the certificate's issuer distinguished name (previously the OCSP URL),
32-
* and the service carries an optional {@link FallbackOcspService}.
32+
* and the service carries an optional {@link ResilientFallbackOcspService}.
3333
*/
3434
class AiaOcspServiceTest {
3535

@@ -88,7 +88,7 @@ void whenFallbackServiceProvided_thenGetFallbackServiceReturnsIt() throws Except
8888
FALLBACK_URI, getDemoEsteidSk2018AiaOcspResponder(), true,
8989
null, getTestEsteid2018CA(), trustedCaAnchors, trustedCaCertStore,
9090
DEFAULT_THIS_UPDATE_AGE, DEFAULT_NEXT_UPDATE_AGE, true);
91-
FallbackOcspService fallback = new FallbackOcspService(fallbackConfiguration);
91+
ResilientFallbackOcspService fallback = new ResilientFallbackOcspService(fallbackConfiguration);
9292

9393
AiaOcspService service = new AiaOcspService(configuration, esteid2018UserCert, fallback);
9494

0 commit comments

Comments
 (0)