jdk/src/share/classes/sun/security/validator/PKIXValidator.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2926 6fbaec35c792
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.validator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Validator implementation built on the PKIX CertPath API. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * implementation will be emphasized going forward.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Note that the validate() implementation tries to use a PKIX validator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * if that appears possible and a PKIX builder otherwise. This increases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * performance and currently also leads to better exception messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * in case of failures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public final class PKIXValidator extends Validator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // enable use of the validator if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final static boolean TRY_VALIDATOR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final Set<X509Certificate> trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final PKIXBuilderParameters parameterTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int certPathLength = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // needed only for the validator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Map<X500Principal, X509Certificate> trustedSubjects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private CertificateFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean plugin = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    PKIXValidator(String variant, Collection<X509Certificate> trustedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        super(TYPE_PKIX, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (trustedCerts instanceof Set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            this.trustedCerts = (Set<X509Certificate>)trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.trustedCerts = new HashSet<X509Certificate>(trustedCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        for (X509Certificate cert : trustedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            trustAnchors.add(new TrustAnchor(cert, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            parameterTemplate = new PKIXBuilderParameters(trustAnchors, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new RuntimeException("Unexpected error: " + e.toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        setDefaultParameters(variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        initCommon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    PKIXValidator(String variant, PKIXBuilderParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super(TYPE_PKIX, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        trustedCerts = new HashSet<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        for (TrustAnchor anchor : params.getTrustAnchors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            X509Certificate cert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                trustedCerts.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        parameterTemplate = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        initCommon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private void initCommon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (TRY_VALIDATOR == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        trustedSubjects = new HashMap<X500Principal, X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (X509Certificate cert : trustedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            trustedSubjects.put(cert.getSubjectX500Principal(), cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new RuntimeException("Internal error", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        plugin = variant.equals(VAR_PLUGIN_CODE_SIGNING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public Collection<X509Certificate> getTrustedCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns the length of the last certification path that is validated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * CertPathValidator. This is intended primarily as a callback mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * for PKIXCertPathCheckers to determine the length of the certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * path that is being validated. It is necessary since engineValidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * may modify the length of the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return the length of the last certification path passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *   CertPathValidator.validate, or -1 if it has not been invoked yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public int getCertPathLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return certPathLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Set J2SE global default PKIX parameters. Currently, hardcoded to disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * revocation checking. In the future, this should be configurable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void setDefaultParameters(String variant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        parameterTemplate.setRevocationEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Return the PKIX parameters used by this instance. An application may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * modify the parameters but must make sure not to perform any concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * validations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public PKIXBuilderParameters getParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return parameterTemplate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    X509Certificate[] engineValidate(X509Certificate[] chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Collection<X509Certificate> otherCerts, Object parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if ((chain == null) || (chain.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                ("null or zero-length certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (TRY_VALIDATOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // check if chain contains trust anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (trustedCerts.contains(chain[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        return new X509Certificate[] {chain[0]};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // Remove and call validator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    X509Certificate[] newChain = new X509Certificate[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    System.arraycopy(chain, 0, newChain, 0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    return doValidate(newChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // not self issued and apparently issued by trust anchor?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            X509Certificate last = chain[chain.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            X500Principal issuer = last.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            X500Principal subject = last.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (trustedSubjects.containsKey(issuer) && !issuer.equals(subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                && isSignatureValid(trustedSubjects.get(issuer), last)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return doValidate(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // don't fallback to builder if called from plugin/webstart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (plugin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                // Validate chain even if no trust anchor is found. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                // allows plugin/webstart to make sure the chain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                // otherwise valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (chain.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    X509Certificate[] newChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        new X509Certificate[chain.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    System.arraycopy(chain, 0, newChain, 0, newChain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    // temporarily set last cert as sole trust anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    PKIXBuilderParameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        (PKIXBuilderParameters) parameterTemplate.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        params.setTrustAnchors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                            (Collections.singleton(new TrustAnchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                (chain[chain.length-1], null)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        // should never occur, but ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        throw new CertificateException(iape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    doValidate(newChain, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // if the rest of the chain is valid, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // indicating no trust anchor was found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    (ValidatorException.T_NO_TRUST_ANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // otherwise, fall back to builder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return doBuild(chain, otherCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private boolean isSignatureValid(X509Certificate iss, X509Certificate sub) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (plugin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                sub.verify(iss.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return true; // only check if PLUGIN is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static X509Certificate[] toArray(CertPath path, TrustAnchor anchor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        List<? extends java.security.cert.Certificate> list =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                                path.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        X509Certificate[] chain = new X509Certificate[list.size() + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        list.toArray(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        X509Certificate trustedCert = anchor.getTrustedCert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (trustedCert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                ("TrustAnchor must be specified as certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        chain[chain.length - 1] = trustedCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Set the check date (for debugging).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private void setDate(PKIXBuilderParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Date date = validationDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (date != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            params.setDate(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private X509Certificate[] doValidate(X509Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        PKIXBuilderParameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            (PKIXBuilderParameters)parameterTemplate.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return doValidate(chain, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private X509Certificate[] doValidate(X509Certificate[] chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            PKIXBuilderParameters params) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            setDate(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // do the validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            CertPathValidator validator = CertPathValidator.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            CertPath path = factory.generateCertPath(Arrays.asList(chain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            certPathLength = chain.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            PKIXCertPathValidatorResult result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                (PKIXCertPathValidatorResult)validator.validate(path, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return toArray(path, result.getTrustAnchor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                ("PKIX path validation failed: " + e.toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private X509Certificate[] doBuild(X509Certificate[] chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        Collection<X509Certificate> otherCerts) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            PKIXBuilderParameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                (PKIXBuilderParameters)parameterTemplate.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            setDate(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // setup target constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            X509CertSelector selector = new X509CertSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            selector.setCertificate(chain[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            params.setTargetCertConstraints(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // setup CertStores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Collection<X509Certificate> certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                        new ArrayList<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            certs.addAll(Arrays.asList(chain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (otherCerts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                certs.addAll(otherCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            CertStore store = CertStore.getInstance("Collection",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                new CollectionCertStoreParameters(certs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            params.addCertStore(store);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // do the build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            PKIXCertPathBuilderResult result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                (PKIXCertPathBuilderResult)builder.build(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return toArray(result.getCertPath(), result.getTrustAnchor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                ("PKIX path building failed: " + e.toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}