1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/ssl/AuthSSLX509TrustManager.java,v 1.2 2004/06/10 18:25:24 olegk Exp $
  3. * $Revision: 1.2 $
  4. * $Date: 2004/06/10 18:25:24 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2002-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.contrib.ssl;
  30. import java.security.cert.X509Certificate;
  31. import com.sun.net.ssl.X509TrustManager;
  32. import org.apache.commons.logging.Log;
  33. import org.apache.commons.logging.LogFactory;
  34. /**
  35. * <p>
  36. * AuthSSLX509TrustManager can be used to extend the default {@link X509TrustManager}
  37. * with additional trust decisions.
  38. * </p>
  39. *
  40. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  41. *
  42. * <p>
  43. * DISCLAIMER: HttpClient developers DO NOT actively support this component.
  44. * The component is provided as a reference material, which may be inappropriate
  45. * for use without additional customization.
  46. * </p>
  47. */
  48. public class AuthSSLX509TrustManager implements X509TrustManager
  49. {
  50. private X509TrustManager defaultTrustManager = null;
  51. /** Log object for this class. */
  52. private static final Log LOG = LogFactory.getLog(AuthSSLX509TrustManager.class);
  53. /**
  54. * Constructor for AuthSSLX509TrustManager.
  55. */
  56. public AuthSSLX509TrustManager(final X509TrustManager defaultTrustManager) {
  57. super();
  58. if (defaultTrustManager == null) {
  59. throw new IllegalArgumentException("Trust manager may not be null");
  60. }
  61. this.defaultTrustManager = defaultTrustManager;
  62. }
  63. /**
  64. * @see com.sun.net.ssl.X509TrustManager#isClientTrusted(X509Certificate[])
  65. */
  66. public boolean isClientTrusted(X509Certificate[] certificates) {
  67. if (LOG.isInfoEnabled() && certificates != null) {
  68. for (int c = 0; c < certificates.length; c++) {
  69. X509Certificate cert = certificates[c];
  70. LOG.info(" Client certificate " + (c + 1) + ":");
  71. LOG.info(" Subject DN: " + cert.getSubjectDN());
  72. LOG.info(" Signature Algorithm: " + cert.getSigAlgName());
  73. LOG.info(" Valid from: " + cert.getNotBefore() );
  74. LOG.info(" Valid until: " + cert.getNotAfter());
  75. LOG.info(" Issuer: " + cert.getIssuerDN());
  76. }
  77. }
  78. return this.defaultTrustManager.isClientTrusted(certificates);
  79. }
  80. /**
  81. * @see com.sun.net.ssl.X509TrustManager#isServerTrusted(X509Certificate[])
  82. */
  83. public boolean isServerTrusted(X509Certificate[] certificates) {
  84. if (LOG.isInfoEnabled() && certificates != null) {
  85. for (int c = 0; c < certificates.length; c++) {
  86. X509Certificate cert = certificates[c];
  87. LOG.info(" Server certificate " + (c + 1) + ":");
  88. LOG.info(" Subject DN: " + cert.getSubjectDN());
  89. LOG.info(" Signature Algorithm: " + cert.getSigAlgName());
  90. LOG.info(" Valid from: " + cert.getNotBefore() );
  91. LOG.info(" Valid until: " + cert.getNotAfter());
  92. LOG.info(" Issuer: " + cert.getIssuerDN());
  93. }
  94. }
  95. return this.defaultTrustManager.isServerTrusted(certificates);
  96. }
  97. /**
  98. * @see com.sun.net.ssl.X509TrustManager#getAcceptedIssuers()
  99. */
  100. public X509Certificate[] getAcceptedIssuers() {
  101. return this.defaultTrustManager.getAcceptedIssuers();
  102. }
  103. }