| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.apache.commons.httpclient.contrib.ssl; |
| 17 | |
|
| 18 | |
import java.security.KeyStore; |
| 19 | |
import java.security.KeyStoreException; |
| 20 | |
import java.security.NoSuchAlgorithmException; |
| 21 | |
import java.security.cert.CertificateException; |
| 22 | |
import java.security.cert.X509Certificate; |
| 23 | |
|
| 24 | |
import javax.net.ssl.TrustManagerFactory; |
| 25 | |
import javax.net.ssl.TrustManager; |
| 26 | |
import javax.net.ssl.X509TrustManager; |
| 27 | |
import org.apache.commons.logging.Log; |
| 28 | |
import org.apache.commons.logging.LogFactory; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public class EasyX509TrustManager implements X509TrustManager |
| 53 | |
{ |
| 54 | 0 | private X509TrustManager standardTrustManager = null; |
| 55 | |
|
| 56 | |
|
| 57 | 0 | private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException { |
| 63 | 0 | super(); |
| 64 | 0 | TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 65 | 0 | factory.init(keystore); |
| 66 | 0 | TrustManager[] trustmanagers = factory.getTrustManagers(); |
| 67 | 0 | if (trustmanagers.length == 0) { |
| 68 | 0 | throw new NoSuchAlgorithmException("no trust manager found"); |
| 69 | |
} |
| 70 | 0 | this.standardTrustManager = (X509TrustManager)trustmanagers[0]; |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public void checkClientTrusted(X509Certificate[] certificates,String authType) throws CertificateException { |
| 77 | 0 | standardTrustManager.checkClientTrusted(certificates,authType); |
| 78 | 0 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public void checkServerTrusted(X509Certificate[] certificates,String authType) throws CertificateException { |
| 84 | 0 | if ((certificates != null) && LOG.isDebugEnabled()) { |
| 85 | 0 | LOG.debug("Server certificate chain:"); |
| 86 | 0 | for (int i = 0; i < certificates.length; i++) { |
| 87 | 0 | LOG.debug("X509Certificate[" + i + "]=" + certificates[i]); |
| 88 | |
} |
| 89 | |
} |
| 90 | 0 | if ((certificates != null) && (certificates.length == 1)) { |
| 91 | 0 | certificates[0].checkValidity(); |
| 92 | |
} else { |
| 93 | 0 | standardTrustManager.checkServerTrusted(certificates,authType); |
| 94 | |
} |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public X509Certificate[] getAcceptedIssuers() { |
| 101 | 0 | return this.standardTrustManager.getAcceptedIssuers(); |
| 102 | |
} |
| 103 | |
} |