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. package org.apache.commons.httpclient.contrib.ssl;
  26. import java.security.KeyStore;
  27. import java.security.KeyStoreException;
  28. import java.security.NoSuchAlgorithmException;
  29. import java.security.cert.CertificateException;
  30. import java.security.cert.X509Certificate;
  31. import com.sun.net.ssl.TrustManagerFactory;
  32. import com.sun.net.ssl.TrustManager;
  33. import com.sun.net.ssl.X509TrustManager;
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. /**
  37. * <p>
  38. * EasyX509TrustManager unlike default {@link X509TrustManager} accepts
  39. * self-signed certificates.
  40. * </p>
  41. * <p>
  42. * This trust manager SHOULD NOT be used for productive systems
  43. * due to security reasons, unless it is a concious decision and
  44. * you are perfectly aware of security implications of accepting
  45. * self-signed certificates
  46. * </p>
  47. *
  48. * @author <a href="mailto:adrian.sutton@ephox.com">Adrian Sutton</a>
  49. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  50. *
  51. * <p>
  52. * DISCLAIMER: HttpClient developers DO NOT actively support this component.
  53. * The component is provided as a reference material, which may be inappropriate
  54. * for use without additional customization.
  55. * </p>
  56. */
  57. public class EasyX509TrustManager implements X509TrustManager
  58. {
  59. private X509TrustManager standardTrustManager = null;
  60. /** Log object for this class. */
  61. private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class);
  62. /**
  63. * Constructor for EasyX509TrustManager.
  64. */
  65. public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
  66. super();
  67. TrustManagerFactory factory = TrustManagerFactory.getInstance("SunX509");
  68. factory.init(keystore);
  69. TrustManager[] trustmanagers = factory.getTrustManagers();
  70. if (trustmanagers.length == 0) {
  71. throw new NoSuchAlgorithmException("SunX509 trust manager not supported");
  72. }
  73. this.standardTrustManager = (X509TrustManager)trustmanagers[0];
  74. }
  75. /**
  76. * @see com.sun.net.ssl.X509TrustManager#isClientTrusted(X509Certificate[])
  77. */
  78. public boolean isClientTrusted(X509Certificate[] certificates) {
  79. return this.standardTrustManager.isClientTrusted(certificates);
  80. }
  81. /**
  82. * @see com.sun.net.ssl.X509TrustManager#isServerTrusted(X509Certificate[])
  83. */
  84. public boolean isServerTrusted(X509Certificate[] certificates) {
  85. if ((certificates != null) && LOG.isDebugEnabled()) {
  86. LOG.debug("Server certificate chain:");
  87. for (int i = 0; i < certificates.length; i++) {
  88. LOG.debug("X509Certificate[" + i + "]=" + certificates[i]);
  89. }
  90. }
  91. if ((certificates != null) && (certificates.length == 1)) {
  92. X509Certificate certificate = certificates[0];
  93. try {
  94. certificate.checkValidity();
  95. }
  96. catch (CertificateException e) {
  97. LOG.error(e.toString());
  98. return false;
  99. }
  100. return true;
  101. } else {
  102. return this.standardTrustManager.isServerTrusted(certificates);
  103. }
  104. }
  105. /**
  106. * @see com.sun.net.ssl.X509TrustManager#getAcceptedIssuers()
  107. */
  108. public X509Certificate[] getAcceptedIssuers() {
  109. return this.standardTrustManager.getAcceptedIssuers();
  110. }
  111. }