1 | /* |
2 | * ==================================================================== |
3 | * |
4 | * Copyright 2002-2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | * |
19 | * This software consists of voluntary contributions made by many |
20 | * individuals on behalf of the Apache Software Foundation. For more |
21 | * information on the Apache Software Foundation, please see |
22 | * <http://www.apache.org/>. |
23 | * |
24 | */ |
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 | /** |
41 | * <p> |
42 | * EasyX509TrustManager unlike default {@link X509TrustManager} accepts |
43 | * self-signed certificates. |
44 | * </p> |
45 | * <p> |
46 | * This trust manager SHOULD NOT be used for productive systems |
47 | * due to security reasons, unless it is a concious decision and |
48 | * you are perfectly aware of security implications of accepting |
49 | * self-signed certificates |
50 | * </p> |
51 | * |
52 | * @author <a href="mailto:adrian.sutton@ephox.com">Adrian Sutton</a> |
53 | * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> |
54 | * |
55 | * <p> |
56 | * DISCLAIMER: HttpClient developers DO NOT actively support this component. |
57 | * The component is provided as a reference material, which may be inappropriate |
58 | * for use without additional customization. |
59 | * </p> |
60 | */ |
61 | |
62 | public class EasyX509TrustManager implements X509TrustManager |
63 | { |
64 | private X509TrustManager standardTrustManager = null; |
65 | |
66 | /** Log object for this class. */ |
67 | private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class); |
68 | |
69 | /** |
70 | * Constructor for EasyX509TrustManager. |
71 | */ |
72 | public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException { |
73 | super(); |
74 | TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509"); |
75 | factory.init(keystore); |
76 | TrustManager[] trustmanagers = factory.getTrustManagers(); |
77 | if (trustmanagers.length == 0) { |
78 | throw new NoSuchAlgorithmException("SunX509 trust manager not supported"); |
79 | } |
80 | this.standardTrustManager = (X509TrustManager)trustmanagers[0]; |
81 | } |
82 | |
83 | /** |
84 | * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String) |
85 | */ |
86 | public void checkClientTrusted(X509Certificate[] chain, String authType) |
87 | throws CertificateException |
88 | { |
89 | this.standardTrustManager.checkClientTrusted(chain, authType); |
90 | } |
91 | |
92 | /** |
93 | * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String) |
94 | */ |
95 | public void checkServerTrusted(X509Certificate[] chain, String authType) |
96 | throws CertificateException { |
97 | if ((chain != null) && LOG.isDebugEnabled()) { |
98 | LOG.debug("Server certificate chain:"); |
99 | for (int i = 0; i < chain.length; i++) { |
100 | LOG.debug("X509Certificate[" + i + "]=" + chain[i]); |
101 | } |
102 | } |
103 | if ((chain != null) && (chain.length == 1)) { |
104 | X509Certificate certificate = chain[0]; |
105 | certificate.checkValidity(); |
106 | } else { |
107 | this.standardTrustManager.checkServerTrusted(chain, authType); |
108 | } |
109 | } |
110 | |
111 | /** |
112 | * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() |
113 | */ |
114 | public X509Certificate[] getAcceptedIssuers() { |
115 | return this.standardTrustManager.getAcceptedIssuers(); |
116 | } |
117 | } |