1 25 26 package org.apache.commons.httpclient.contrib.ssl; 27 28 import java.security.KeyStore ; 29 import java.security.KeyStoreException ; 30 import java.security.NoSuchAlgorithmException ; 31 import java.security.cert.CertificateException ; 32 import java.security.cert.X509Certificate ; 33 34 import javax.net.ssl.TrustManagerFactory; 35 import javax.net.ssl.TrustManager; 36 import javax.net.ssl.X509TrustManager; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 61 62 public class EasyX509TrustManager implements X509TrustManager 63 { 64 private X509TrustManager standardTrustManager = null; 65 66 67 private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class); 68 69 72 public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException , KeyStoreException { 73 super(); 74 TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 75 factory.init(keystore); 76 TrustManager[] trustmanagers = factory.getTrustManagers(); 77 if (trustmanagers.length == 0) { 78 throw new NoSuchAlgorithmException ("no trust manager found"); 79 } 80 this.standardTrustManager = (X509TrustManager)trustmanagers[0]; 81 } 82 83 86 public void checkClientTrusted(X509Certificate [] certificates,String authType) throws CertificateException { 87 standardTrustManager.checkClientTrusted(certificates,authType); 88 } 89 90 93 public void checkServerTrusted(X509Certificate [] certificates,String authType) throws CertificateException { 94 if ((certificates != null) && LOG.isDebugEnabled()) { 95 LOG.debug("Server certificate chain:"); 96 for (int i = 0; i < certificates.length; i++) { 97 LOG.debug("X509Certificate[" + i + "]=" + certificates[i]); 98 } 99 } 100 if ((certificates != null) && (certificates.length == 1)) { 101 certificates[0].checkValidity(); 102 } else { 103 standardTrustManager.checkServerTrusted(certificates,authType); 104 } 105 } 106 107 110 public X509Certificate [] getAcceptedIssuers() { 111 return this.standardTrustManager.getAcceptedIssuers(); 112 } 113 } 114 | Popular Tags |