View Javadoc

1   /***
2    * Simple Web Spider - <http://simplewebspider.sourceforge.net/>
3    * Copyright (C) 2009  <berendona@users.sourceforge.net>
4    *
5    * This program is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package simplespider.simplespider.bot.http.apache.ssl;
19  
20  import java.security.KeyStore;
21  import java.security.KeyStoreException;
22  import java.security.NoSuchAlgorithmException;
23  import java.security.cert.CertificateException;
24  import java.security.cert.X509Certificate;
25  
26  import javax.net.ssl.TrustManager;
27  import javax.net.ssl.TrustManagerFactory;
28  import javax.net.ssl.X509TrustManager;
29  
30  public class TrustAllX509TrustManager implements X509TrustManager {
31  	private X509TrustManager	standardTrustManager	= null;
32  
33  	/*** Log object for this class. */
34  	// private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class);
35  	/***
36  	 * Constructor for EasyX509TrustManager.
37  	 */
38  	public TrustAllX509TrustManager(final KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
39  		super();
40  		final TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
41  		factory.init(keystore);
42  		final TrustManager[] trustmanagers = factory.getTrustManagers();
43  		if (trustmanagers.length == 0) {
44  			throw new NoSuchAlgorithmException("no trust manager found");
45  		}
46  		this.standardTrustManager = (X509TrustManager) trustmanagers[0];
47  	}
48  
49  	/***
50  	 * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
51  	 */
52  	public void checkClientTrusted(final X509Certificate[] certificates, final String authType) throws CertificateException {
53  		// this.standardTrustManager.checkClientTrusted(certificates, authType);
54  	}
55  
56  	/***
57  	 * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
58  	 */
59  	public void checkServerTrusted(final X509Certificate[] certificates, final String authType) throws CertificateException {
60  		// if ((certificates != null) && LOG.isDebugEnabled()) {
61  		// LOG.debug("Server certificate chain:");
62  		// for (int i = 0; i < certificates.length; i++) {
63  		// LOG.debug("X509Certificate[" + i + "]=" + certificates[i]);
64  		// }
65  		// }
66  		// if ((certificates != null) && (certificates.length == 1)) {
67  		// certificates[0].checkValidity();
68  		// } else {
69  		// this.standardTrustManager.checkServerTrusted(certificates, authType);
70  		// }
71  	}
72  
73  	/***
74  	 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
75  	 */
76  	public X509Certificate[] getAcceptedIssuers() {
77  		return this.standardTrustManager.getAcceptedIssuers();
78  	}
79  }