|
| 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 | +// } |
0 commit comments