EasyX509TrustManager.java
2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.crx.packaging.impl;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EasyX509TrustManager
implements X509TrustManager {
private X509TrustManager standardTrustManager = null;
private boolean allowExpired;
private static final Logger log = LoggerFactory.getLogger(EasyX509TrustManager.class);
public EasyX509TrustManager(KeyStore keystore, boolean allowExpired) throws NoSuchAlgorithmException, KeyStoreException {
this.allowExpired = allowExpired;
TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
factory.init(keystore);
TrustManager[] trustmanagers = factory.getTrustManagers();
if (trustmanagers.length == 0) {
throw new NoSuchAlgorithmException("no trust manager found");
}
this.standardTrustManager = (X509TrustManager)trustmanagers[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
this.standardTrustManager.checkClientTrusted(certificates, authType);
}
@Override
public void checkServerTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
if (certificates != null && log.isDebugEnabled()) {
log.debug("Server certificate chain:");
for (int i = 0; i < certificates.length; ++i) {
log.debug("X509Certificate[" + i + "]=" + certificates[i]);
}
}
if (certificates != null && certificates.length == 1) {
if (!this.allowExpired) {
certificates[0].checkValidity();
}
} else {
this.standardTrustManager.checkServerTrusted(certificates, authType);
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return this.standardTrustManager.getAcceptedIssuers();
}
}