1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: import ;
65: import ;
66: import ;
67: import ;
68:
69: import ;
70: import ;
71: import ;
72: import ;
73:
74:
78: public class X509TrustManagerFactory extends TrustManagerFactorySpi
79: {
80:
81:
82:
83:
84: private static final String sep
85: = AccessController.doPrivileged(new GetPropertyAction("file.separator"));
86:
87:
90: private static final String JSSE_CERTS
91: = AccessController.doPrivileged(new GetPropertyAction("java.home"))
92: + sep + "lib" + sep + "security" + sep + "jssecerts";
93:
94:
97: private static final String CA_CERTS
98: = AccessController.doPrivileged(new GetPropertyAction("java.home"))
99: + sep + "lib" + sep + "security" + sep + "cacerts";
100:
101: private Manager current;
102:
103:
104:
105:
106: public X509TrustManagerFactory()
107: {
108: super();
109: }
110:
111:
112:
113:
114: protected TrustManager[] engineGetTrustManagers()
115: {
116: if (current == null)
117: {
118: throw new IllegalStateException("not initialized");
119: }
120: return new TrustManager[] { current };
121: }
122:
123: protected void engineInit(ManagerFactoryParameters params)
124: throws InvalidAlgorithmParameterException
125: {
126: if (params instanceof StaticTrustAnchors)
127: {
128: current = new Manager(((StaticTrustAnchors) params).getCertificates());
129: }
130: else if (params instanceof NullManagerParameters)
131: {
132: current = new Manager(new X509Certificate[0]);
133: }
134: else
135: {
136: throw new InvalidAlgorithmParameterException();
137: }
138: }
139:
140: protected void engineInit(KeyStore store) throws KeyStoreException
141: {
142: if (store == null)
143: {
144: GetPropertyAction gpa = new GetPropertyAction("javax.net.ssl.trustStoreType");
145: String s = AccessController.doPrivileged(gpa);
146: if (s == null)
147: s = KeyStore.getDefaultType();
148: store = KeyStore.getInstance(s);
149: try
150: {
151: s = AccessController.doPrivileged(gpa.setParameters("javax.net.ssl.trustStore"));
152: FileInputStream in = null;
153: if (s == null)
154: {
155: try
156: {
157: in = new FileInputStream(JSSE_CERTS);
158: }
159: catch (IOException e)
160: {
161: in = new FileInputStream(CA_CERTS);
162: }
163: }
164: else
165: {
166: in = new FileInputStream(s);
167: }
168: String p = AccessController.doPrivileged(gpa.setParameters("javax.net.ssl.trustStorePassword"));
169: store.load(in, p != null ? p.toCharArray() : null);
170: }
171: catch (IOException ioe)
172: {
173: throw new KeyStoreException(ioe);
174: }
175: catch (CertificateException ce)
176: {
177: throw new KeyStoreException(ce);
178: }
179: catch (NoSuchAlgorithmException nsae)
180: {
181: throw new KeyStoreException(nsae);
182: }
183: }
184:
185: LinkedList<X509Certificate> l = new LinkedList<X509Certificate>();
186: Enumeration aliases = store.aliases();
187: while (aliases.hasMoreElements())
188: {
189: String alias = (String) aliases.nextElement();
190: if (!store.isCertificateEntry(alias))
191: continue;
192: Certificate c = store.getCertificate(alias);
193: if (!(c instanceof X509Certificate))
194: continue;
195: l.add((X509Certificate) c);
196: }
197: current = this.new Manager(l.toArray(new X509Certificate[l.size()]));
198: }
199:
200:
201:
202:
203:
206: private class Manager implements X509TrustManager
207: {
208:
209:
210:
211:
212: private final Set<TrustAnchor> anchors;
213:
214:
215:
216:
217: Manager(X509Certificate[] trusted)
218: {
219: anchors = new HashSet<TrustAnchor>();
220: if (trusted != null)
221: {
222: for (X509Certificate cert : trusted)
223: {
224: anchors.add(new TrustAnchor(cert, null));
225: }
226: }
227: }
228:
229:
230:
231:
232: public void checkClientTrusted(X509Certificate[] chain, String authType)
233: throws CertificateException
234: {
235: checkTrusted(chain, authType);
236: }
237:
238: public void checkServerTrusted(X509Certificate[] chain, String authType)
239: throws CertificateException
240: {
241: checkTrusted(chain, authType);
242: }
243:
244: public X509Certificate[] getAcceptedIssuers()
245: {
246: return anchors.toArray(new X509Certificate[anchors.size()]);
247: }
248:
249:
250:
251:
252: private void checkTrusted(X509Certificate[] chain, String authType)
253: throws CertificateException
254: {
255: CertPathValidator validator = null;
256:
257: try
258: {
259: validator = CertPathValidator.getInstance("PKIX");
260: }
261: catch (NoSuchAlgorithmException nsae)
262: {
263: throw new CertificateException(nsae);
264: }
265:
266: CertPath path = new X509CertPath(Arrays.asList(chain));
267:
268: PKIXParameters params = null;
269: try
270: {
271: params = new PKIXParameters(anchors);
272:
273:
274: params.setRevocationEnabled(false);
275: }
276: catch (InvalidAlgorithmParameterException iape)
277: {
278: throw new CertificateException(iape);
279: }
280:
281: try
282: {
283: validator.validate(path, params);
284: }
285: catch (CertPathValidatorException cpve)
286: {
287: throw new CertificateException(cpve);
288: }
289: catch (InvalidAlgorithmParameterException iape)
290: {
291: throw new CertificateException(iape);
292: }
293: }
294: }
295: }