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 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 public class EasyX509TrustManager implements X509TrustManager {
34 private X509TrustManager standardTrustManager = null;
35
36 /*** Log object for this class. */
37 private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class);
38
39 /***
40 * Constructor for EasyX509TrustManager.
41 */
42 public EasyX509TrustManager(final KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
43 super();
44 final TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
45 factory.init(keystore);
46 final TrustManager[] trustmanagers = factory.getTrustManagers();
47 if (trustmanagers.length == 0) {
48 throw new NoSuchAlgorithmException("no trust manager found");
49 }
50 this.standardTrustManager = (X509TrustManager) trustmanagers[0];
51 }
52
53 /***
54 * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
55 */
56 public void checkClientTrusted(final X509Certificate[] certificates, final String authType) throws CertificateException {
57
58 }
59
60 /***
61 * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
62 */
63 public void checkServerTrusted(final X509Certificate[] certificates, final String authType) throws CertificateException {
64 if ((certificates != null) && LOG.isDebugEnabled()) {
65 LOG.debug("Server certificate chain:");
66 for (int i = 0; i < certificates.length; i++) {
67 LOG.debug("X509Certificate[" + i + "]=" + certificates[i]);
68 }
69 }
70 if ((certificates != null) && (certificates.length == 1)) {
71 certificates[0].checkValidity();
72 } else {
73 this.standardTrustManager.checkServerTrusted(certificates, authType);
74 }
75 }
76
77 /***
78 * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
79 */
80 public X509Certificate[] getAcceptedIssuers() {
81 return this.standardTrustManager.getAcceptedIssuers();
82 }
83 }