blob: 92850d33ae6b0257a21c15c14947d8419bff3aa5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 * NOTE: this file was copied from javax.net.ssl.TrustManagerFactory
28 */
29
30package com.sun.net.ssl;
31
32import java.security.*;
33
34/**
35 * This class acts as a factory for trust managers based on a
36 * source of trust material. Each trust manager manages a specific
37 * type of trust material for use by secure sockets. The trust
38 * material is based on a KeyStore and/or provider specific sources.
39 *
40 * @deprecated As of JDK 1.4, this implementation-specific class was
41 * replaced by {@link javax.net.ssl.TrustManagerFactory}.
42 */
43@Deprecated
44public class TrustManagerFactory {
45 // The provider
46 private Provider provider;
47
48 // The provider implementation (delegate)
49 private TrustManagerFactorySpi factorySpi;
50
51 // The name of the trust management algorithm.
52 private String algorithm;
53
54 /**
55 * <p>The default TrustManager can be changed by setting the value of the
56 * "sun.ssl.trustmanager.type" security property
57 * (in the Java security properties file) to the desired name.
58 *
59 * @return the default type as specified in the
60 * Java security properties file, or an implementation-specific default
61 * if no such property exists.
62 */
63 public final static String getDefaultAlgorithm() {
64 String type;
65 type = AccessController.doPrivileged(new PrivilegedAction<String>() {
66 public String run() {
67 return Security.getProperty("sun.ssl.trustmanager.type");
68 }
69 });
70 if (type == null) {
71 type = "SunX509";
72 }
73 return type;
74
75 }
76
77 /**
78 * Creates a TrustManagerFactory object.
79 *
80 * @param factorySpi the delegate
81 * @param provider the provider
82 * @param algorithm the algorithm
83 */
84 protected TrustManagerFactory(TrustManagerFactorySpi factorySpi,
85 Provider provider, String algorithm) {
86 this.factorySpi = factorySpi;
87 this.provider = provider;
88 this.algorithm = algorithm;
89 }
90
91 /**
92 * Returns the algorithm name of this <code>TrustManagerFactory</code>
93 * object.
94 *
95 * <p>This is the same name that was specified in one of the
96 * <code>getInstance</code> calls that created this
97 * <code>TrustManagerFactory</code> object.
98 *
99 * @return the algorithm name of this <code>TrustManagerFactory</code>
100 * object.
101 */
102 public final String getAlgorithm() {
103 return this.algorithm;
104 }
105
106 /**
107 * Generates a <code>TrustManagerFactory</code> object that implements the
108 * specified trust management algorithm.
109 * If the default provider package provides an implementation of the
110 * requested trust management algorithm, an instance of
111 * <code>TrustManagerFactory</code> containing that implementation is
112 * returned. If the algorithm is not available in the default provider
113 * package, other provider packages are searched.
114 *
115 * @param algorithm the standard name of the requested trust management
116 * algorithm.
117 *
118 * @return the new <code>TrustManagerFactory</code> object
119 *
120 * @exception NoSuchAlgorithmException if the specified algorithm is not
121 * available in the default provider package or any of the other provider
122 * packages that were searched.
123 */
124 public static final TrustManagerFactory getInstance(String algorithm)
125 throws NoSuchAlgorithmException
126 {
127 try {
128 Object[] objs = SSLSecurity.getImpl(algorithm,
129 "TrustManagerFactory", (String) null);
130 return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
131 (Provider)objs[1],
132 algorithm);
133 } catch (NoSuchProviderException e) {
134 throw new NoSuchAlgorithmException(algorithm + " not found");
135 }
136 }
137
138 /**
139 * Generates a <code>TrustManagerFactory</code> object for the specified
140 * trust management algorithm from the specified provider.
141 *
142 * @param algorithm the standard name of the requested trust management
143 * algorithm.
144 * @param provider the name of the provider
145 *
146 * @return the new <code>TrustManagerFactory</code> object
147 *
148 * @exception NoSuchAlgorithmException if the specified algorithm is not
149 * available from the specified provider.
150 * @exception NoSuchProviderException if the specified provider has not
151 * been configured.
152 */
153 public static final TrustManagerFactory getInstance(String algorithm,
154 String provider)
155 throws NoSuchAlgorithmException, NoSuchProviderException
156 {
157 if (provider == null || provider.length() == 0)
158 throw new IllegalArgumentException("missing provider");
159 Object[] objs = SSLSecurity.getImpl(algorithm, "TrustManagerFactory",
160 provider);
161 return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
162 (Provider)objs[1], algorithm);
163 }
164
165 /**
166 * Generates a <code>TrustManagerFactory</code> object for the specified
167 * trust management algorithm from the specified provider.
168 *
169 * @param algorithm the standard name of the requested trust management
170 * algorithm.
171 * @param provider an instance of the provider
172 *
173 * @return the new <code>TrustManagerFactory</code> object
174 *
175 * @exception NoSuchAlgorithmException if the specified algorithm is not
176 * available from the specified provider.
177 */
178 public static final TrustManagerFactory getInstance(String algorithm,
179 Provider provider)
180 throws NoSuchAlgorithmException
181 {
182 if (provider == null)
183 throw new IllegalArgumentException("missing provider");
184 Object[] objs = SSLSecurity.getImpl(algorithm, "TrustManagerFactory",
185 provider);
186 return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
187 (Provider)objs[1], algorithm);
188 }
189
190 /**
191 * Returns the provider of this <code>TrustManagerFactory</code> object.
192 *
193 * @return the provider of this <code>TrustManagerFactory</code> object
194 */
195 public final Provider getProvider() {
196 return this.provider;
197 }
198
199
200 /**
201 * Initializes this factory with a source of certificate
202 * authorities and related trust material. The
203 * provider may also include a provider-specific source
204 * of key material.
205 *
206 * @param ks the key store or null
207 */
208 public void init(KeyStore ks) throws KeyStoreException {
209 factorySpi.engineInit(ks);
210 }
211
212 /**
213 * Returns one trust manager for each type of trust material.
214 * @return the trust managers
215 */
216 public TrustManager[] getTrustManagers() {
217 return factorySpi.engineGetTrustManagers();
218 }
219}