2323package eu .webeid .ocsp .service ;
2424
2525import eu .webeid .ocsp .exceptions .OCSPCertificateException ;
26+ import eu .webeid .ocsp .protocol .OcspResponseValidator ;
27+ import eu .webeid .security .certificate .CertificateValidator ;
2628import eu .webeid .security .exceptions .AuthTokenException ;
29+ import eu .webeid .security .validator .revocationcheck .RevocationMode ;
2730import org .bouncycastle .cert .X509CertificateHolder ;
2831import org .bouncycastle .cert .jcajce .JcaX509CertificateConverter ;
2932
3033import java .net .URI ;
34+ import java .security .cert .CertStore ;
3135import java .security .cert .CertificateException ;
36+ import java .security .cert .TrustAnchor ;
3237import java .security .cert .X509Certificate ;
3338import java .util .Date ;
39+ import java .util .Set ;
3440
3541
3642import static eu .webeid .security .certificate .CertificateValidator .requireCertificateIsValidOnDate ;
@@ -42,6 +48,8 @@ public class FallbackOcspService implements OcspService {
4248 private final boolean supportsNonce ;
4349 private final X509Certificate trustedResponderCertificate ;
4450 private final FallbackOcspService nextFallback ;
51+ private final Set <TrustAnchor > trustedCACertificateAnchors ;
52+ private final CertStore trustedCACertificateCertStore ;
4553
4654 public FallbackOcspService (FallbackOcspServiceConfiguration configuration ) {
4755 this .url = configuration .getAccessLocation ();
@@ -50,6 +58,8 @@ public FallbackOcspService(FallbackOcspServiceConfiguration configuration) {
5058 this .nextFallback = configuration .getNextFallbackConfiguration () != null
5159 ? new FallbackOcspService (configuration .getNextFallbackConfiguration ())
5260 : null ;
61+ this .trustedCACertificateAnchors = configuration .getTrustedCACertificateAnchors ();
62+ this .trustedCACertificateCertStore = configuration .getTrustedCACertificateCertStore ();
5363 }
5464
5565 @ Override
@@ -66,13 +76,26 @@ public URI getAccessLocation() {
6676 public void validateResponderCertificate (X509CertificateHolder cert , Date now ) throws AuthTokenException {
6777 try {
6878 final X509Certificate responderCertificate = certificateConverter .getCertificate (cert );
69- // Certificate pinning is implemented simply by comparing the certificates or their public keys,
70- // see https://owasp.org/www-community/controls/Certificate_and_Public_Key_Pinning.
71- if (!trustedResponderCertificate .equals (responderCertificate )) {
72- throw new OCSPCertificateException ("Responder certificate from the OCSP response is not equal to " +
73- "the configured fallback OCSP responder certificate" );
74- }
7579 requireCertificateIsValidOnDate (responderCertificate , now , "Fallback OCSP responder" );
80+ if (trustedResponderCertificate != null ) {
81+ // Certificate pinning is implemented simply by comparing the certificates or their public keys,
82+ // see https://owasp.org/www-community/controls/Certificate_and_Public_Key_Pinning.
83+ if (!trustedResponderCertificate .equals (responderCertificate )) {
84+ throw new OCSPCertificateException ("Responder certificate from the OCSP response is not equal to " +
85+ "the configured fallback OCSP responder certificate" );
86+ }
87+ return ;
88+ }
89+ OcspResponseValidator .validateHasSigningExtension (responderCertificate );
90+ CertificateValidator .validateCertificateTrustAndRevocation (
91+ responderCertificate ,
92+ trustedCACertificateAnchors ,
93+ trustedCACertificateCertStore ,
94+ now ,
95+ RevocationMode .DISABLED ,
96+ null ,
97+ null
98+ );
7699 } catch (CertificateException e ) {
77100 throw new OCSPCertificateException ("X509CertificateHolder conversion to X509Certificate failed" , e );
78101 }
0 commit comments