TrustManagerFactory.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* TrustManagerFactory.java -- factory for trust managers.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.net.ssl;
  32. import gnu.java.security.Engine;
  33. import java.lang.reflect.InvocationTargetException;
  34. import java.security.AccessController;
  35. import java.security.InvalidAlgorithmParameterException;
  36. import java.security.KeyStore;
  37. import java.security.KeyStoreException;
  38. import java.security.NoSuchAlgorithmException;
  39. import java.security.NoSuchProviderException;
  40. import java.security.PrivilegedAction;
  41. import java.security.Provider;
  42. import java.security.Security;
  43. /**
  44. * A factory for creating trust manager objects.
  45. */
  46. public class TrustManagerFactory
  47. {
  48. // Constants and fields.
  49. // -------------------------------------------------------------------------
  50. /** The service name for trust manager factories. */
  51. private static final String TRUST_MANAGER_FACTORY = "TrustManagerFactory";
  52. /** The system default trust manager algorithm. */
  53. private static final String DEFAULT_ALGORITHM = "JessieX509";
  54. /** The underlying engine class. */
  55. private final TrustManagerFactorySpi tmfSpi;
  56. /** The provider of the engine class. */
  57. private final Provider provider;
  58. /** The name of this trust manager algorithm. */
  59. private final String algorithm;
  60. // Constructor.
  61. // -------------------------------------------------------------------------
  62. /**
  63. * Creates a new trust manager factory.
  64. *
  65. * @param tmfSpi The underlying engine class.
  66. * @param provider The provider of the engine class.
  67. * @param algorithm The trust manager algorithm name.
  68. */
  69. protected TrustManagerFactory(TrustManagerFactorySpi tmfSpi,
  70. Provider provider, String algorithm)
  71. {
  72. this.tmfSpi = tmfSpi;
  73. this.provider = provider;
  74. this.algorithm = algorithm;
  75. }
  76. /**
  77. * Returns an instance of a trust manager factory for the given algorithm from
  78. * the first provider that implements it.
  79. *
  80. * @param algorithm The name of the algorithm to get.
  81. * @return The instance of the trust manager factory.
  82. * @throws NoSuchAlgorithmException If no provider implements the given
  83. * algorithm.
  84. * @throws IllegalArgumentException if <code>algorithm</code> is
  85. * <code>null</code> or is an empty string.
  86. */
  87. public static final TrustManagerFactory getInstance(String algorithm)
  88. throws NoSuchAlgorithmException
  89. {
  90. Provider[] p = Security.getProviders();
  91. NoSuchAlgorithmException lastException = null;
  92. for (int i = 0; i < p.length; i++)
  93. try
  94. {
  95. return getInstance(algorithm, p[i]);
  96. }
  97. catch (NoSuchAlgorithmException x)
  98. {
  99. lastException = x;
  100. }
  101. if (lastException != null)
  102. throw lastException;
  103. throw new NoSuchAlgorithmException(algorithm);
  104. }
  105. /**
  106. * Returns an instance of a trust manager factory for the given algorithm from
  107. * the named provider.
  108. *
  109. * @param algorithm The name of the algorithm to get.
  110. * @param provider The name of the provider to get the instance from.
  111. * @return The instance of the trust manager factory.
  112. * @throws NoSuchAlgorithmException If the provider does not implement the
  113. * given algorithm.
  114. * @throws NoSuchProviderException If there is no such named provider.
  115. * @throws IllegalArgumentException if either <code>algorithm</code> or
  116. * <code>provider</code> is <code>null</code>, or if
  117. * <code>algorithm</code> is an empty string.
  118. */
  119. public static final TrustManagerFactory getInstance(String algorithm,
  120. String provider)
  121. throws NoSuchAlgorithmException, NoSuchProviderException
  122. {
  123. if (provider == null)
  124. throw new IllegalArgumentException("provider MUST NOT be null");
  125. Provider p = Security.getProvider(provider);
  126. if (p == null)
  127. throw new NoSuchProviderException(provider);
  128. return getInstance(algorithm, p);
  129. }
  130. /**
  131. * Returns an instance of a trust manager factory for the given algorithm from
  132. * the specified provider.
  133. *
  134. * @param algorithm The name of the algorithm to get.
  135. * @param provider The provider to get the instance from.
  136. * @return The instance of the trust manager factory.
  137. * @throws NoSuchAlgorithmException If the provider does not implement the
  138. * given algorithm.
  139. * @throws IllegalArgumentException if either <code>algorithm</code> or
  140. * <code>provider</code> is <code>null</code>, or if
  141. * <code>algorithm</code> is an empty string.
  142. */
  143. public static final TrustManagerFactory getInstance(String algorithm,
  144. Provider provider)
  145. throws NoSuchAlgorithmException
  146. {
  147. StringBuilder sb = new StringBuilder("TrustManagerFactory algorithm [")
  148. .append(algorithm).append("] from provider[")
  149. .append(provider).append("] could not be created");
  150. Throwable cause;
  151. try
  152. {
  153. Object spi = Engine.getInstance(TRUST_MANAGER_FACTORY, algorithm, provider);
  154. return new TrustManagerFactory((TrustManagerFactorySpi) spi,
  155. provider,
  156. algorithm);
  157. }
  158. catch (InvocationTargetException x)
  159. {
  160. cause = x.getCause();
  161. if (cause instanceof NoSuchAlgorithmException)
  162. throw (NoSuchAlgorithmException) cause;
  163. if (cause == null)
  164. cause = x;
  165. }
  166. catch (ClassCastException x)
  167. {
  168. cause = x;
  169. }
  170. NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
  171. x.initCause(cause);
  172. throw x;
  173. }
  174. /**
  175. * Returns the default algorithm for trust manager factories. The value
  176. * returned is either the value of the security property
  177. * "ssl.TrustManagerFactory.algorithm" if it is set, or the value "JessieX509"
  178. * if not.
  179. *
  180. * @return The default algorithm name.
  181. * @see Security.getProperty(java.lang.String)
  182. */
  183. public static final String getDefaultAlgorithm()
  184. {
  185. String alg = null;
  186. try
  187. {
  188. alg = (String) AccessController.doPrivileged(
  189. new PrivilegedAction()
  190. {
  191. public Object run()
  192. {
  193. return Security.getProperty("ssl.TrustManagerFactory.algorithm");
  194. }
  195. }
  196. );
  197. }
  198. catch (SecurityException se)
  199. {
  200. }
  201. if (alg == null)
  202. alg = DEFAULT_ALGORITHM;
  203. return alg;
  204. }
  205. // Instance methods.
  206. // -------------------------------------------------------------------------
  207. /**
  208. * Returns the name of this trust manager algorithm.
  209. *
  210. * @return The algorithm name.
  211. */
  212. public final String getAlgorithm()
  213. {
  214. return algorithm;
  215. }
  216. /**
  217. * Returns the provider of the underlying implementation.
  218. *
  219. * @return The provider.
  220. */
  221. public final Provider getProvider()
  222. {
  223. return provider;
  224. }
  225. /**
  226. * Returns the trust managers created by this factory.
  227. *
  228. * @return The trust managers.
  229. */
  230. public final TrustManager[] getTrustManagers()
  231. {
  232. return tmfSpi.engineGetTrustManagers();
  233. }
  234. /**
  235. * Initialize this instance with some algorithm-specific parameters.
  236. *
  237. * @param params The parameters.
  238. * @throws InvalidAlgorithmParameterException If the supplied parameters
  239. * are inappropriate for this instance.
  240. */
  241. public final void init(ManagerFactoryParameters params)
  242. throws InvalidAlgorithmParameterException
  243. {
  244. tmfSpi.engineInit(params);
  245. }
  246. /**
  247. * Initialize this instance with a key store. The key store may be null,
  248. * in which case a default will be used.
  249. *
  250. * @param store The key store.
  251. * @throws KeyStoreException If there is a problem reading from the
  252. * key store.
  253. */
  254. public final void init(KeyStore store) throws KeyStoreException
  255. {
  256. tmfSpi.engineInit(store);
  257. }
  258. }