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