Skip to content

Commit 02c7887

Browse files
committed
refactor(SubjectCertificateTrustedValidator): locate the CA certificate by using subject certificate issuer X.500 principal before signature verification (#5)
1 parent 1a78169 commit 02c7887

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/main/java/org/webeid/security/validator/validators/SubjectCertificateTrustedValidator.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,24 @@
2727
import org.webeid.security.exceptions.UserCertificateNotTrustedException;
2828
import org.webeid.security.validator.AuthTokenValidatorData;
2929

30+
import javax.security.auth.x500.X500Principal;
3031
import java.security.GeneralSecurityException;
3132
import java.security.cert.X509Certificate;
3233
import java.util.Collection;
34+
import java.util.Map;
35+
import java.util.function.Function;
36+
import java.util.stream.Collectors;
3337

3438
public 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() {

src/test/java/org/webeid/security/validator/TrustedCaTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ protected void setup() {
5151
@Test
5252
void detectUntrustedUserCertificate() {
5353
assertThatThrownBy(() -> validator.validate(Tokens.SIGNED))
54-
.isInstanceOf(UserCertificateNotTrustedException.class);
54+
.isInstanceOf(UserCertificateNotTrustedException.class)
55+
.hasMessageStartingWith("User certificate is not trusted: User certificate CA is not in the trusted CA list");
5556
}
5657

5758
}

0 commit comments

Comments
 (0)