001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/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 $
003: * $Revision$
004: * $Date$
005: *
006: * ====================================================================
007: *
008: * Copyright 2002-2004 The Apache Software Foundation
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient.contrib.ssl;
031:
032: import java.security.cert.X509Certificate;
033:
034: import javax.net.ssl.X509TrustManager;
035: import java.security.cert.CertificateException;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * <p>
041: * AuthSSLX509TrustManager can be used to extend the default {@link X509TrustManager}
042: * with additional trust decisions.
043: * </p>
044: *
045: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
046: *
047: * <p>
048: * DISCLAIMER: HttpClient developers DO NOT actively support this component.
049: * The component is provided as a reference material, which may be inappropriate
050: * for use without additional customization.
051: * </p>
052: */
053:
054: public class AuthSSLX509TrustManager implements X509TrustManager {
055: private X509TrustManager defaultTrustManager = null;
056:
057: /** Log object for this class. */
058: private static final Log LOG = LogFactory
059: .getLog(AuthSSLX509TrustManager.class);
060:
061: /**
062: * Constructor for AuthSSLX509TrustManager.
063: */
064: public AuthSSLX509TrustManager(
065: final X509TrustManager defaultTrustManager) {
066: super ();
067: if (defaultTrustManager == null) {
068: throw new IllegalArgumentException(
069: "Trust manager may not be null");
070: }
071: this .defaultTrustManager = defaultTrustManager;
072: }
073:
074: /**
075: * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
076: */
077: public void checkClientTrusted(X509Certificate[] certificates,
078: String authType) throws CertificateException {
079: if (LOG.isInfoEnabled() && certificates != null) {
080: for (int c = 0; c < certificates.length; c++) {
081: X509Certificate cert = certificates[c];
082: LOG.info(" Client certificate " + (c + 1) + ":");
083: LOG.info(" Subject DN: " + cert.getSubjectDN());
084: LOG.info(" Signature Algorithm: "
085: + cert.getSigAlgName());
086: LOG.info(" Valid from: " + cert.getNotBefore());
087: LOG.info(" Valid until: " + cert.getNotAfter());
088: LOG.info(" Issuer: " + cert.getIssuerDN());
089: }
090: }
091: defaultTrustManager.checkClientTrusted(certificates, authType);
092: }
093:
094: /**
095: * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
096: */
097: public void checkServerTrusted(X509Certificate[] certificates,
098: String authType) throws CertificateException {
099: if (LOG.isInfoEnabled() && certificates != null) {
100: for (int c = 0; c < certificates.length; c++) {
101: X509Certificate cert = certificates[c];
102: LOG.info(" Server certificate " + (c + 1) + ":");
103: LOG.info(" Subject DN: " + cert.getSubjectDN());
104: LOG.info(" Signature Algorithm: "
105: + cert.getSigAlgName());
106: LOG.info(" Valid from: " + cert.getNotBefore());
107: LOG.info(" Valid until: " + cert.getNotAfter());
108: LOG.info(" Issuer: " + cert.getIssuerDN());
109: }
110: }
111: defaultTrustManager.checkServerTrusted(certificates, authType);
112: }
113:
114: /**
115: * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
116: */
117: public X509Certificate[] getAcceptedIssuers() {
118: return this.defaultTrustManager.getAcceptedIssuers();
119: }
120: }
|