Skip to content

Commit 775b476

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

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

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

+40-18
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,25 +111,49 @@ 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+
}
153+
}else {
154+
throw new CertificateException("Certificate chain cannot be trusted");
128155
}
129156
}
130-
// check if have basic constraints
131157
}
132158

133159
@Override
@@ -195,9 +221,7 @@ private void validatePath(X509Certificate[] chain)
195221
loadCRL(chain);
196222
parameters.addCertStore(crlStore);
197223
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
224+
// check OCSP, important reminder if CRL is disabled then then OCSP will not work either
201225
// parameters.setCertPathCheckers(checkers);
202226
}
203227

@@ -290,8 +314,6 @@ private void loadTrustStore() {
290314
private void loadCRL(X509Certificate[] chain) throws IOException, InvalidAlgorithmParameterException,
291315
NoSuchAlgorithmException, CertStoreException, CRLException, CertificateException {
292316

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

0 commit comments

Comments
 (0)