2727import eu .webeid .resilientocsp .service .FallbackOcspServiceConfiguration ;
2828import eu .webeid .security .exceptions .AuthTokenException ;
2929
30- import java .net .URI ;
3130import java .security .cert .CertificateEncodingException ;
3231import java .security .cert .X509Certificate ;
3332import java .util .Collection ;
33+ import java .util .HashMap ;
3434import java .util .Map ;
3535import java .util .Objects ;
36- import java .util .stream .Collectors ;
3736
38- import static eu .webeid .ocsp .protocol .OcspUrl . getOcspUri ;
37+ import static eu .webeid .ocsp .protocol .IssuerCommonName . getIssuerCommonName ;
3938
4039public class OcspServiceProvider {
4140
4241 private final DesignatedOcspService designatedOcspService ;
4342 private final AiaOcspServiceConfiguration aiaOcspServiceConfiguration ;
44- private final Map <URI , FallbackOcspService > fallbackOcspServiceMap ;
43+ private final Map <String , FallbackOcspService > fallbackOcspServiceMap = new HashMap <>() ;
4544
4645 public OcspServiceProvider (DesignatedOcspServiceConfiguration designatedOcspServiceConfiguration , AiaOcspServiceConfiguration aiaOcspServiceConfiguration ) {
4746 this (designatedOcspServiceConfiguration , aiaOcspServiceConfiguration , null );
@@ -52,9 +51,13 @@ public OcspServiceProvider(DesignatedOcspServiceConfiguration designatedOcspServ
5251 new DesignatedOcspService (designatedOcspServiceConfiguration )
5352 : null ;
5453 this .aiaOcspServiceConfiguration = Objects .requireNonNull (aiaOcspServiceConfiguration , "aiaOcspServiceConfiguration" );
55- this .fallbackOcspServiceMap = fallbackOcspServiceConfigurations != null ? fallbackOcspServiceConfigurations .stream ()
56- .collect (Collectors .toMap (FallbackOcspServiceConfiguration ::getOcspServiceAccessLocation , FallbackOcspService ::new ))
57- : Map .of ();
54+ if (fallbackOcspServiceConfigurations != null ) {
55+ for (FallbackOcspServiceConfiguration configuration : fallbackOcspServiceConfigurations ) {
56+ String issuerCN = getIssuerCommonName (configuration .getResponderCertificate ()).orElseThrow (() ->
57+ new RuntimeException ("Certificate does not contain issuer CN" ));
58+ fallbackOcspServiceMap .put (issuerCN , new FallbackOcspService (configuration ));
59+ }
60+ }
5861 }
5962
6063 /**
@@ -63,20 +66,16 @@ public OcspServiceProvider(DesignatedOcspServiceConfiguration designatedOcspServ
6366 *
6467 * @param certificate subject certificate that is to be checked with OCSP
6568 * @return either the designated or AIA OCSP service instance
66- * @throws AuthTokenException when AIA URL is not found in certificate
69+ * @throws UserCertificateOCSPCheckFailedException when issuer common name is not found in certificate
6770 * @throws IllegalArgumentException when certificate is invalid
6871 */
6972 public OcspService getService (X509Certificate certificate ) throws AuthTokenException , CertificateEncodingException {
7073 if (designatedOcspService != null && designatedOcspService .supportsIssuerOf (certificate )) {
7174 return designatedOcspService ;
7275 }
73- URI ocspServiceUri = getOcspUri (certificate ).orElseThrow (() ->
74- new UserCertificateOCSPCheckFailedException ("Getting the AIA OCSP responder field from the certificate failed" ));
75- FallbackOcspService fallbackOcspService = fallbackOcspServiceMap .get (ocspServiceUri );
76+ String issuerCommonName = getIssuerCommonName (certificate ).orElseThrow (() ->
77+ new UserCertificateOCSPCheckFailedException ("Getting the issuer common name failed" ));
78+ FallbackOcspService fallbackOcspService = fallbackOcspServiceMap .get (issuerCommonName );
7679 return new AiaOcspService (aiaOcspServiceConfiguration , certificate , fallbackOcspService );
7780 }
78-
79- public FallbackOcspService getFallbackService (URI ocspServiceUri ) {
80- return fallbackOcspServiceMap .get (ocspServiceUri );
81- }
8281}
0 commit comments