27
27
import org .webeid .security .exceptions .UserCertificateNotTrustedException ;
28
28
import org .webeid .security .validator .AuthTokenValidatorData ;
29
29
30
+ import javax .security .auth .x500 .X500Principal ;
30
31
import java .security .GeneralSecurityException ;
31
32
import java .security .cert .X509Certificate ;
32
33
import java .util .Collection ;
34
+ import java .util .Map ;
35
+ import java .util .function .Function ;
36
+ import java .util .stream .Collectors ;
33
37
34
38
public final class SubjectCertificateTrustedValidator {
35
39
36
40
private static final Logger LOG = LoggerFactory .getLogger (SubjectCertificateTrustedValidator .class );
37
41
38
- private final Collection < X509Certificate > trustedCACertificates ;
42
+ private final Map < X500Principal , X509Certificate > trustedCACertificates ;
39
43
private X509Certificate trustedCACertificate ;
40
44
41
45
public SubjectCertificateTrustedValidator (Collection <X509Certificate > trustedCACertificates ) {
42
- this .trustedCACertificates = trustedCACertificates ;
46
+ this .trustedCACertificates = trustedCACertificates .stream ()
47
+ .collect (Collectors .toMap (X509Certificate ::getSubjectX500Principal , Function .identity ()));
43
48
}
44
49
45
50
/**
@@ -50,22 +55,24 @@ public SubjectCertificateTrustedValidator(Collection<X509Certificate> trustedCAC
50
55
*/
51
56
public void validateCertificateTrusted (AuthTokenValidatorData actualTokenData ) throws UserCertificateNotTrustedException {
52
57
53
- final X509Certificate certificate = actualTokenData .getSubjectCertificate ();
58
+ final X509Certificate userCertificate = actualTokenData .getSubjectCertificate ();
59
+ final X509Certificate caCertificate = trustedCACertificates .get (userCertificate .getIssuerX500Principal ());
54
60
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" );
66
69
}
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 ();
67
75
}
68
- throw new UserCertificateNotTrustedException ();
69
76
}
70
77
71
78
public X509Certificate getSubjectCertificateIssuerCertificate () {
0 commit comments