ClearspaceX509TrustManager.java 9 KB
Newer Older
1 2 3 4 5 6 7
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */

package org.jivesoftware.openfire.clearspace;

import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import javax.net.ssl.X509TrustManager;

import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.CertificateManager;
35 36
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
37 38 39 40 41 42 43 44 45 46

/**
 * Trust manager that validates Clearspace certificates. Using system properties
 * it is possible to disable or enabled certain validations. By default all validations
 * are enabled and self-signed certificated are not accepted.
 *
 * @author Gaston Dombiak
 */
public class ClearspaceX509TrustManager implements X509TrustManager {

47
	private static final Logger Log = LoggerFactory.getLogger(ClearspaceX509TrustManager.class);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    /**
     * KeyStore that holds the trusted CA
     */
    private KeyStore trustStore;
    private String server;
    private Map<String, String> properties;

    public ClearspaceX509TrustManager(String server, Map<String, String> properties, KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
        super();
        this.server = server;
        this.trustStore = keystore;
        this.properties = properties;
    }


    /**
     * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
     */
66
    @Override
67 68 69 70 71 72 73
    public void checkClientTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
        // Do nothing. We are the client so we are not testing certificates from clients
    }

    /**
     * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
     */
74
    @Override
75 76 77 78 79 80 81
    public void checkServerTrusted(X509Certificate[] x509Certificates, String authType) throws CertificateException {
        // Flag that indicates if certificates of the remote server should be validated. Disabling
        // certificate validation is not recommended for production environments.
        boolean verify = getBooleanProperty("clearspace.certificate.verify", true);
        if (verify) {
            int nSize = x509Certificates.length;

Victor Hong's avatar
Victor Hong committed
82
            List<String> peerIdentities = CertificateManager.getServerIdentities(x509Certificates[0]);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

            if (getBooleanProperty("clearspace.certificate.verify.chain", true)) {
                // Working down the chain, for every certificate in the chain,
                // verify that the subject of the certificate is the issuer of the
                // next certificate in the chain.
                Principal principalLast = null;
                for (int i = nSize -1; i >= 0 ; i--) {
                    X509Certificate x509certificate = x509Certificates[i];
                    Principal principalIssuer = x509certificate.getIssuerDN();
                    Principal principalSubject = x509certificate.getSubjectDN();
                    if (principalLast != null) {
                        if (principalIssuer.equals(principalLast)) {
                            try {
                                PublicKey publickey =
                                        x509Certificates[i + 1].getPublicKey();
                                x509Certificates[i].verify(publickey);
                            }
                            catch (GeneralSecurityException generalsecurityexception) {
                                throw new CertificateException(
                                        "signature verification failed of " + peerIdentities);
                            }
                        }
                        else {
                            throw new CertificateException(
                                    "subject/issuer verification failed of " + peerIdentities);
                        }
                    }
                    principalLast = principalSubject;
                }
            }

            if (getBooleanProperty("clearspace.certificate.verify.root", true)) {
                // Verify that the the last certificate in the chain was issued
                // by a third-party that the client trusts.
                boolean trusted = false;
                try {
                    trusted = trustStore.getCertificateAlias(x509Certificates[nSize - 1]) != null;
                    if (!trusted && nSize == 1 && JiveGlobals
                            .getBooleanProperty("clearspace.certificate.accept-selfsigned", false))
                    {
                        Log.warn("Accepting self-signed certificate of remote server: " +
                                peerIdentities);
                        trusted = true;
                    }
                }
                catch (KeyStoreException e) {
129
                    Log.error(e.getMessage(), e);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
                }
                if (!trusted) {
                    throw new CertificateException("root certificate not trusted of " + peerIdentities);
                }
            }

            if (getBooleanProperty("clearspace.certificate.verify.identity", false)) {
                // Verify that the first certificate in the chain corresponds to
                // the server we desire to authenticate.
                // Check if the certificate uses a wildcard indicating that subdomains are valid
                if (peerIdentities.size() == 1 && peerIdentities.get(0).startsWith("*.")) {
                    // Remove the wildcard
                    String peerIdentity = peerIdentities.get(0).replace("*.", "");
                    // Check if the requested subdomain matches the certified domain
                    if (!server.endsWith(peerIdentity)) {
                        throw new CertificateException("target verification failed of " + peerIdentities);
                    }
                }
                else if (!peerIdentities.contains(server)) {
                    throw new CertificateException("target verification failed of " + peerIdentities);
                }
            }

            if (getBooleanProperty("clearspace.certificate.verify.validity", true)) {
                // For every certificate in the chain, verify that the certificate
                // is valid at the current time.
                Date date = new Date();
                for (int i = 0; i < nSize; i++) {
                    try {
                        x509Certificates[i].checkValidity(date);
                    }
                    catch (GeneralSecurityException generalsecurityexception) {
                        throw new CertificateException("invalid date of " + peerIdentities);
                    }
                }
            }
        }
    }

    private boolean getBooleanProperty(String key, boolean defaultValue) {
        String value = properties.get(key);
        return value != null ? Boolean.parseBoolean(value) : defaultValue;
    }

    /**
     * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
     */
177
    @Override
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    public X509Certificate[] getAcceptedIssuers() {
        if (getBooleanProperty("clearspace.certificate.accept-selfsigned", false)) {
            // Answer an empty list since we accept any issuer
            return new X509Certificate[0];
        }
        else {
            X509Certificate[] X509Certs = null;
            try {
                // See how many certificates are in the keystore.
                int numberOfEntry = trustStore.size();
                // If there are any certificates in the keystore.
                if (numberOfEntry > 0) {
                    // Create an array of X509Certificates
                    X509Certs = new X509Certificate[numberOfEntry];

                    // Get all of the certificate alias out of the keystore.
                    Enumeration aliases = trustStore.aliases();

                    // Retrieve all of the certificates out of the keystore
                    // via the alias name.
                    int i = 0;
                    while (aliases.hasMoreElements()) {
                        X509Certs[i] =
                                (X509Certificate) trustStore.
                                        getCertificate((String) aliases.nextElement());
                        i++;
                    }

                }
            }
            catch (Exception e) {
209
                Log.error(e.getMessage(), e);
210 211 212 213 214 215
                X509Certs = null;
            }
            return X509Certs;
        }
    }
}