2727import org .webeid .security .exceptions .UserCertificateNotTrustedException ;
2828import org .webeid .security .validator .AuthTokenValidatorData ;
2929
30+ import javax .security .auth .x500 .X500Principal ;
3031import java .security .GeneralSecurityException ;
3132import java .security .cert .X509Certificate ;
3233import java .util .Collection ;
34+ import java .util .Map ;
35+ import java .util .function .Function ;
36+ import java .util .stream .Collectors ;
3337
3438public final class SubjectCertificateTrustedValidator {
3539
3640 private static final Logger LOG = LoggerFactory .getLogger (SubjectCertificateTrustedValidator .class );
3741
38- private final Collection < X509Certificate > trustedCACertificates ;
42+ private final Map < X500Principal , X509Certificate > trustedCACertificates ;
3943 private X509Certificate trustedCACertificate ;
4044
4145 public SubjectCertificateTrustedValidator (Collection <X509Certificate > trustedCACertificates ) {
42- this .trustedCACertificates = trustedCACertificates ;
46+ this .trustedCACertificates = trustedCACertificates .stream ()
47+ .collect (Collectors .toMap (X509Certificate ::getSubjectX500Principal , Function .identity ()));
4348 }
4449
4550 /**
@@ -50,22 +55,24 @@ public SubjectCertificateTrustedValidator(Collection<X509Certificate> trustedCAC
5055 */
5156 public void validateCertificateTrusted (AuthTokenValidatorData actualTokenData ) throws UserCertificateNotTrustedException {
5257
53- final X509Certificate certificate = actualTokenData .getSubjectCertificate ();
58+ final X509Certificate userCertificate = actualTokenData .getSubjectCertificate ();
59+ final X509Certificate caCertificate = trustedCACertificates .get (userCertificate .getIssuerX500Principal ());
5460
55- for (final X509Certificate caCertificate : trustedCACertificates ) {
56- try {
57- certificate .verify (caCertificate .getPublicKey ());
58- if (certificate .getNotAfter ().after (caCertificate .getNotAfter ())) {
59- throw new UserCertificateNotTrustedException ("Trusted CA certificate expires earlier than the user certificate" );
60- }
61- this .trustedCACertificate = caCertificate ;
62- LOG .debug ("User certificate is signed with a trusted CA certificate" );
63- return ;
64- } catch (GeneralSecurityException e ) {
65- LOG .trace ("Error verifying signer's certificate {} against CA certificate {}" , certificate .getSubjectDN (), caCertificate .getSubjectDN ());
61+ if (caCertificate == null ) {
62+ throw new UserCertificateNotTrustedException ("User certificate CA is not in the trusted CA list" );
63+ }
64+
65+ try {
66+ userCertificate .verify (caCertificate .getPublicKey ());
67+ if (userCertificate .getNotAfter ().after (caCertificate .getNotAfter ())) {
68+ throw new UserCertificateNotTrustedException ("Trusted CA certificate expires earlier than the user certificate" );
6669 }
70+ this .trustedCACertificate = caCertificate ;
71+ LOG .debug ("User certificate is signed with a trusted CA certificate" );
72+ } catch (GeneralSecurityException e ) {
73+ LOG .trace ("Error verifying signer's certificate {} against CA certificate {}" , userCertificate .getSubjectDN (), caCertificate .getSubjectDN ());
74+ throw new UserCertificateNotTrustedException ();
6775 }
68- throw new UserCertificateNotTrustedException ();
6976 }
7077
7178 public X509Certificate getSubjectCertificateIssuerCertificate () {
0 commit comments