|
32 | 32 | import java.security.cert.X509CertSelector;
|
33 | 33 | import java.security.cert.X509Certificate;
|
34 | 34 | import java.util.ArrayList;
|
| 35 | +import java.util.Arrays; |
35 | 36 | import java.util.Collection;
|
36 | 37 | import java.util.Enumeration;
|
37 | 38 | import java.util.List;
|
@@ -71,6 +72,7 @@ public class SparkTrustManager implements X509TrustManager {
|
71 | 72 | private boolean acceptSelfSigned;
|
72 | 73 |
|
73 | 74 | private CertStore crlStore;
|
| 75 | + private Collection<X509CRL> crlCollection = new ArrayList<>(); |
74 | 76 | private X509TrustManager exceptionsTrustManager;
|
75 | 77 | private Provider bcProvider = new BouncyCastleProvider(); // bc provider for path validation
|
76 | 78 | private KeyStore trustStore;
|
@@ -109,22 +111,45 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws
|
109 | 111 | } catch (CertificateException ex) {
|
110 | 112 | // in case when certificate isn't on exceptions list then make use of this Trust Manager
|
111 | 113 |
|
| 114 | + // validate chain by date (expired/not valid yet) |
| 115 | + checkDateValidity(chain); |
| 116 | + |
112 | 117 | // check if certificate isn't self signed, self signed certificate still have to be in TrustStore to be
|
113 | 118 | // 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); |
117 | 123 |
|
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); |
120 | 128 |
|
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 | + } |
128 | 153 | }
|
129 | 154 | }
|
130 | 155 | // check if have basic constraints
|
@@ -195,9 +220,7 @@ private void validatePath(X509Certificate[] chain)
|
195 | 220 | loadCRL(chain);
|
196 | 221 | parameters.addCertStore(crlStore);
|
197 | 222 | 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 |
201 | 224 | // parameters.setCertPathCheckers(checkers);
|
202 | 225 | }
|
203 | 226 |
|
@@ -290,8 +313,6 @@ private void loadTrustStore() {
|
290 | 313 | private void loadCRL(X509Certificate[] chain) throws IOException, InvalidAlgorithmParameterException,
|
291 | 314 | NoSuchAlgorithmException, CertStoreException, CRLException, CertificateException {
|
292 | 315 |
|
293 |
| - Collection<X509CRL> crlCollection = new ArrayList<>(); |
294 |
| - |
295 | 316 | // for each certificate in chain
|
296 | 317 | for (X509Certificate cert : chain) {
|
297 | 318 | if (cert.getExtensionValue(Extension.cRLDistributionPoints.getId()) != null) {
|
|
0 commit comments