2020 * SOFTWARE.
2121 */
2222
23- package eu .webeid .resilientocsp .service ;
23+ package eu .webeid .ocsp .service ;
2424
2525import eu .webeid .ocsp .exceptions .OCSPCertificateException ;
26- import eu .webeid .ocsp .service .OcspService ;
26+ import eu .webeid .ocsp .protocol .OcspResponseValidator ;
27+ import eu .webeid .security .certificate .CertificateValidator ;
2728import eu .webeid .security .exceptions .AuthTokenException ;
29+ import eu .webeid .security .validator .revocationcheck .RevocationMode ;
2830import org .bouncycastle .cert .X509CertificateHolder ;
2931import org .bouncycastle .cert .jcajce .JcaX509CertificateConverter ;
3032
3133import java .net .URI ;
34+ import java .security .cert .CertStore ;
3235import java .security .cert .CertificateException ;
36+ import java .security .cert .TrustAnchor ;
3337import java .security .cert .X509Certificate ;
3438import java .util .Date ;
39+ import java .util .Set ;
3540
3641
3742import static eu .webeid .security .certificate .CertificateValidator .requireCertificateIsValidOnDate ;
@@ -42,11 +47,19 @@ public class FallbackOcspService implements OcspService {
4247 private final URI url ;
4348 private final boolean supportsNonce ;
4449 private final X509Certificate trustedResponderCertificate ;
50+ private final FallbackOcspService nextFallback ;
51+ private final Set <TrustAnchor > trustedCACertificateAnchors ;
52+ private final CertStore trustedCACertificateCertStore ;
4553
4654 public FallbackOcspService (FallbackOcspServiceConfiguration configuration ) {
47- this .url = configuration .getFallbackOcspServiceAccessLocation ();
55+ this .url = configuration .getAccessLocation ();
4856 this .supportsNonce = configuration .doesSupportNonce ();
4957 this .trustedResponderCertificate = configuration .getResponderCertificate ();
58+ this .nextFallback = configuration .getNextFallbackConfiguration () != null
59+ ? new FallbackOcspService (configuration .getNextFallbackConfiguration ())
60+ : null ;
61+ this .trustedCACertificateAnchors = configuration .getTrustedCACertificateAnchors ();
62+ this .trustedCACertificateCertStore = configuration .getTrustedCACertificateCertStore ();
5063 }
5164
5265 @ Override
@@ -63,15 +76,40 @@ public URI getAccessLocation() {
6376 public void validateResponderCertificate (X509CertificateHolder cert , Date now ) throws AuthTokenException {
6477 try {
6578 final X509Certificate responderCertificate = certificateConverter .getCertificate (cert );
66- // Certificate pinning is implemented simply by comparing the certificates or their public keys,
67- // see https://owasp.org/www-community/controls/Certificate_and_Public_Key_Pinning.
68- if (!trustedResponderCertificate .equals (responderCertificate )) {
69- throw new OCSPCertificateException ("Responder certificate from the OCSP response is not equal to " +
70- "the configured fallback OCSP responder certificate" );
71- }
7279 requireCertificateIsValidOnDate (responderCertificate , now , "Fallback OCSP responder" );
80+ if (trustedResponderCertificate != null ) {
81+ validatePinnedResponderCertificate (responderCertificate );
82+ } else {
83+ validateResponderCertificateAgainstTrustedCa (responderCertificate , now );
84+ }
7385 } catch (CertificateException e ) {
7486 throw new OCSPCertificateException ("X509CertificateHolder conversion to X509Certificate failed" , e );
7587 }
7688 }
89+
90+ private void validatePinnedResponderCertificate (X509Certificate responderCertificate ) throws OCSPCertificateException {
91+ // Certificate pinning is implemented simply by comparing the certificates or their public keys,
92+ // see https://owasp.org/www-community/controls/Certificate_and_Public_Key_Pinning.
93+ if (!trustedResponderCertificate .equals (responderCertificate )) {
94+ throw new OCSPCertificateException ("Responder certificate from the OCSP response is not equal to " +
95+ "the configured fallback OCSP responder certificate" );
96+ }
97+ }
98+
99+ private void validateResponderCertificateAgainstTrustedCa (X509Certificate responderCertificate , Date now ) throws AuthTokenException {
100+ OcspResponseValidator .validateHasSigningExtension (responderCertificate );
101+ CertificateValidator .validateCertificateTrustAndRevocation (
102+ responderCertificate ,
103+ trustedCACertificateAnchors ,
104+ trustedCACertificateCertStore ,
105+ now ,
106+ RevocationMode .DISABLED ,
107+ null ,
108+ null
109+ );
110+ }
111+
112+ public FallbackOcspService getNextFallback () {
113+ return nextFallback ;
114+ }
77115}
0 commit comments