Skip to content

Commit eb595f9

Browse files
committed
SelfSigned certificates now can be accepted.
1 parent 81ba4c9 commit eb595f9

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

core/src/main/java/org/jivesoftware/sparkimpl/certificates/SparkTrustManager.java

+38-17
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.security.cert.X509CertSelector;
3333
import java.security.cert.X509Certificate;
3434
import java.util.ArrayList;
35+
import java.util.Arrays;
3536
import java.util.Collection;
3637
import java.util.Enumeration;
3738
import java.util.List;
@@ -71,6 +72,7 @@ public class SparkTrustManager implements X509TrustManager {
7172
private boolean acceptSelfSigned;
7273

7374
private CertStore crlStore;
75+
private Collection<X509CRL> crlCollection = new ArrayList<>();
7476
private X509TrustManager exceptionsTrustManager;
7577
private Provider bcProvider = new BouncyCastleProvider(); // bc provider for path validation
7678
private KeyStore trustStore;
@@ -109,22 +111,45 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws
109111
} catch (CertificateException ex) {
110112
// in case when certificate isn't on exceptions list then make use of this Trust Manager
111113

114+
// validate chain by date (expired/not valid yet)
115+
checkDateValidity(chain);
116+
112117
// check if certificate isn't self signed, self signed certificate still have to be in TrustStore to be
113118
// accepted
114-
if (chain.length == 1 && acceptSelfSigned == false) {
115-
throw new CertificateException("SelfSigned certificate");
116-
}
119+
if (chain.length > 1) {
120+
// validate certificate path
121+
try {
122+
validatePath(chain);
117123

118-
// validate chain by date (expired/not valid yet)
119-
checkDateValidity(chain);
124+
} catch (NoSuchAlgorithmException | KeyStoreException | InvalidAlgorithmParameterException
125+
| CertPathValidatorException | CertPathBuilderException e) {
126+
Log.error("Validating path failed", e);
127+
throw new CertificateException("Certificate path validation failed", e);
120128

121-
// validate certificate path
122-
try {
123-
validatePath(chain);
124-
} catch (NoSuchAlgorithmException | KeyStoreException | InvalidAlgorithmParameterException
125-
| CertPathValidatorException | CertPathBuilderException e) {
126-
Log.error("Validating path failed", e);
127-
throw new CertificateException("Certificate path validation failed", e);
129+
}
130+
} else if (chain.length == 1 && !acceptSelfSigned) {
131+
// Self Signed certificate while it isn't accepted
132+
throw new CertificateException("Self Signed certificate");
133+
134+
} else if (chain.length == 1 && acceptSelfSigned) {
135+
// check if certificate is in Keystore and check CRL, but do not validate path as certificate is Self
136+
// Signed important reminder: hostname validation must be also turned off to accept self signed
137+
// certificate
138+
List<X509Certificate> certList = new ArrayList<>(Arrays.asList(getAcceptedIssuers()));
139+
if (!certList.contains(chain[0])) {
140+
throw new CertificateException("Certificate not in the TrustStore");
141+
}
142+
try {
143+
loadCRL(chain);
144+
for (X509CRL crl : crlCollection) {
145+
if (crl.isRevoked(chain[0])) {
146+
throw new CertificateException("Certificate is revoked");
147+
}
148+
}
149+
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | CertStoreException
150+
| CRLException | IOException e) {
151+
Log.warning("Couldn't load CRL");
152+
}
128153
}
129154
}
130155
// check if have basic constraints
@@ -195,9 +220,7 @@ private void validatePath(X509Certificate[] chain)
195220
loadCRL(chain);
196221
parameters.addCertStore(crlStore);
197222
if (checkOCSP) {
198-
// check OCSP, important reminder if CRL is disabled then then OCSP will not work either, for
199-
// reference:
200-
// http://docs.oracle.com/javase/7/docs/technotes/guides/security/certpath/CertPathProgGuide.html#AppC
223+
// check OCSP, important reminder if CRL is disabled then then OCSP will not work either
201224
// parameters.setCertPathCheckers(checkers);
202225
}
203226

@@ -290,8 +313,6 @@ private void loadTrustStore() {
290313
private void loadCRL(X509Certificate[] chain) throws IOException, InvalidAlgorithmParameterException,
291314
NoSuchAlgorithmException, CertStoreException, CRLException, CertificateException {
292315

293-
Collection<X509CRL> crlCollection = new ArrayList<>();
294-
295316
// for each certificate in chain
296317
for (X509Certificate cert : chain) {
297318
if (cert.getExtensionValue(Extension.cRLDistributionPoints.getId()) != null) {

0 commit comments

Comments
 (0)