    001/*
    002 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.15/src/java/org/apache/commons/ssl/Java14TrustManagerWrapper.java $
    003 * $Revision: 138 $
    004 * $Date: 2008-03-03 23:50:07 -0800 (Mon, 03 Mar 2008) $
    005 *
    006 * ====================================================================
    007 * Licensed to the Apache Software Foundation (ASF) under one
    008 * or more contributor license agreements.  See the NOTICE file
    009 * distributed with this work for additional information
    010 * regarding copyright ownership.  The ASF licenses this file
    011 * to you under the Apache License, Version 2.0 (the
    012 * "License"); you may not use this file except in compliance
    013 * with the License.  You may obtain a copy of the License at
    014 *
    015 *   http://www.apache.org/licenses/LICENSE-2.0
    016 *
    017 * Unless required by applicable law or agreed to in writing,
    018 * software distributed under the License is distributed on an
    019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    020 * KIND, either express or implied.  See the License for the
    021 * specific language governing permissions and limitations
    022 * under the License.
    023 * ====================================================================
    024 *
    025 * This software consists of voluntary contributions made by many
    026 * individuals on behalf of the Apache Software Foundation.  For more
    027 * information on the Apache Software Foundation, please see
    028 * <http://www.apache.org/>.
    029 *
    030 */
    031
    032package org.apache.commons.ssl;
    033
    034import javax.net.ssl.X509TrustManager;
    035import java.security.cert.CertificateException;
    036import java.security.cert.CertificateExpiredException;
    037import java.security.cert.X509Certificate;
    038
    039/**
    040 * @author Credit Union Central of British Columbia
    041 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
    042 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
    043 * @since 30-Mar-2006
    044 */
    045public class Java14TrustManagerWrapper implements X509TrustManager {
    046    private final X509TrustManager trustManager;
    047    private final TrustChain trustChain;
    048    private final SSL ssl;
    049
    050    public Java14TrustManagerWrapper(X509TrustManager m, TrustChain tc, SSL h) {
    051        this.trustManager = m;
    052        this.trustChain = tc;
    053        this.ssl = h;
    054    }
    055
    056    public void checkClientTrusted(X509Certificate[] chain, String authType)
    057        throws CertificateException {
    058        ssl.setCurrentClientChain(chain);
    059        CertificateException ce = null;
    060        try {
    061            trustManager.checkClientTrusted(chain, authType);
    062        }
    063        catch (CertificateException e) {
    064            ce = e;
    065        }
    066        testShouldWeThrow(ce, chain);
    067    }
    068
    069    public void checkServerTrusted(X509Certificate[] chain, String authType)
    070        throws CertificateException {
    071        ssl.setCurrentServerChain(chain);
    072        CertificateException ce = null;
    073        try {
    074            trustManager.checkServerTrusted(chain, authType);
    075        }
    076        catch (CertificateException e) {
    077            ce = e;
    078        }
    079        testShouldWeThrow(ce, chain);
    080    }
    081
    082    public X509Certificate[] getAcceptedIssuers() {
    083        if (trustChain.containsTrustAll()) {
    084            // Counter-intuitively, this means we accept all issuers.
    085            return new X509Certificate[0];
    086        } else {
    087            return trustManager.getAcceptedIssuers();
    088        }
    089    }
    090
    091    private void testShouldWeThrow(CertificateException checkException,
    092                                   X509Certificate[] chain)
    093        throws CertificateException {
    094        if (checkException != null) {
    095            Throwable root = getRootThrowable(checkException);
    096            boolean expiryProblem = root instanceof CertificateExpiredException;
    097            if (expiryProblem) {
    098                if (ssl.getCheckExpiry()) {
    099                    // We're expired, and this factory cares.
    100                    throw checkException;
    101                }
    102            } else {
    103                // Probably the cert isn't trusted.  Only let it through if
    104                // this factory trusts everything.
    105                if (!trustChain.contains(TrustMaterial.TRUST_ALL)) {
    106                    throw checkException;
    107                }
    108            }
    109        }
    110
    111        for (int i = 0; i < chain.length; i++) {
    112            X509Certificate c = chain[i];
    113            if (ssl.getCheckExpiry()) {
    114                c.checkValidity();
    115            }
    116            if (ssl.getCheckCRL()) {
    117                Certificates.checkCRL(c);
    118            }
    119        }
    120    }
    121
    122    private static Throwable getRootThrowable(Throwable t) {
    123        if (t == null) {
    124            return t;
    125        }
    126        Throwable cause = t.getCause();
    127        while (cause != null && !t.equals(cause)) {
    128            t = cause;
    129            cause = t.getCause();
    130        }
    131        return t;
    132    }
    133}