jdk/src/share/classes/sun/security/ssl/X509TrustManagerImpl.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7043 5e2d1edeb2c7
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
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.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.net.ssl.internal.ssl.X509ExtendedTrustManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.validator.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.HostnameChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class implements the SunJSSE X.509 trust manager using the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * validator API in J2SE core. The logic in this class is minimal.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class supports both the Simple validation algorithm from previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * JSSE versions and PKIX validation. Currently, it is not possible for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * application to specify PKIX parameters other than trust anchors. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * be fixed in a future release using new APIs. When that happens, it may also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * make sense to separate the Simple and PKIX trust managers into separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Xuelei Fan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
final class X509TrustManagerImpl extends X509ExtendedTrustManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        implements X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Flag indicating whether to enable revocation check for the PKIX trust
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * manager. Typically, this will only work if the PKIX implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * supports CRL distribution points as we do not manually setup CertStores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static boolean checkRevocation =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        Debug.getBooleanProperty("com.sun.net.ssl.checkRevocation", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final String validatorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The Set of trusted X509Certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final Collection<X509Certificate> trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final PKIXBuilderParameters pkixParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // note that we need separate validator for client and server due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // the different extension checks. They are initialized lazily on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private volatile Validator clientValidator, serverValidator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    X509TrustManagerImpl(String validatorType, KeyStore ks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.validatorType = validatorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.pkixParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            trustedCerts = Collections.<X509Certificate>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            trustedCerts = KeyStores.getTrustedCerts(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        showTrustedCerts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    X509TrustManagerImpl(String validatorType, PKIXBuilderParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.validatorType = validatorType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.pkixParams = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // create server validator eagerly so that we can conveniently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // get the trusted certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // clients need it anyway eventually, and servers will not mind
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // the little extra footprint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Validator v = getValidator(Validator.VAR_TLS_SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        trustedCerts = v.getTrustedCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        serverValidator = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        showTrustedCerts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private void showTrustedCerts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            for (X509Certificate cert : trustedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                System.out.println("adding as trusted cert:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                System.out.println("  Subject: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                        + cert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                System.out.println("  Issuer:  "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                        + cert.getIssuerX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.out.println("  Algorithm: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                        + cert.getPublicKey().getAlgorithm()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                        + "; Serial number: 0x"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                        + cert.getSerialNumber().toString(16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.out.println("  Valid from "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                        + cert.getNotBefore() + " until "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                        + cert.getNotAfter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private Validator getValidator(String variant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Validator v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (pkixParams == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            v = Validator.getInstance(validatorType, variant, trustedCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // if the PKIX validator is created from a KeyStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // disable revocation checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (v instanceof PKIXValidator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                PKIXValidator pkixValidator = (PKIXValidator)v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                pkixValidator.getParameters().setRevocationEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                                            (checkRevocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            v = Validator.getInstance(validatorType, variant, pkixParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static X509Certificate[] validate(Validator v,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            X509Certificate[] chain, String authType) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Object o = JsseJce.beginFipsProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return v.validate(chain, null, authType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            JsseJce.endFipsProvider(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns true if the client certificate can be trusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param chain certificates which establish an identity for the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *      Chains of arbitrary length are supported, and certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *      marked internally as trusted will short-circuit signature checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws IllegalArgumentException if null or zero-length chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *         is passed in for the chain parameter or if null or zero-length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *         string is passed in for the authType parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws CertificateException if the certificate chain is not trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *      by this TrustManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void checkClientTrusted(X509Certificate chain[], String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (chain == null || chain.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                "null or zero-length certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (authType == null || authType.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                "null or zero-length authentication type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // assume double checked locking with a volatile flag works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // (guaranteed under the new Tiger memory model)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        Validator v = clientValidator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                v = clientValidator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    v = getValidator(Validator.VAR_TLS_CLIENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    clientValidator = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        X509Certificate[] trustedChain = validate(v, chain, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            System.out.println("Found trusted certificate:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            System.out.println(trustedChain[trustedChain.length - 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns true if the server certifcate can be trusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param chain certificates which establish an identity for the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *      Chains of arbitrary length are supported, and certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *      marked internally as trusted will short-circuit signature checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @throws IllegalArgumentException if null or zero-length chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *         is passed in for the chain parameter or if null or zero-length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *         string is passed in for the authType parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @throws CertificateException if the certificate chain is not trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *      by this TrustManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public void checkServerTrusted(X509Certificate chain[], String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (chain == null || chain.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                "null or zero-length certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (authType == null || authType.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                "null or zero-length authentication type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // assume double checked locking with a volatile flag works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // (guaranteed under the new Tiger memory model)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        Validator v = serverValidator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                v = serverValidator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    v = getValidator(Validator.VAR_TLS_SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    serverValidator = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        X509Certificate[] trustedChain = validate(v, chain, authType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            System.out.println("Found trusted certificate:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            System.out.println(trustedChain[trustedChain.length - 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Returns a list of CAs accepted to authenticate entities for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * specified purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param purpose activity for which CAs should be trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return list of CAs accepted for authenticating such tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        X509Certificate[] certsArray = new X509Certificate[trustedCerts.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        trustedCerts.toArray(certsArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return certsArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Given the partial or complete certificate chain provided by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * peer, check its identity and build a certificate path to a trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * root, return if it can be validated and is trusted for client SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * authentication based on the authentication type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public void checkClientTrusted(X509Certificate[] chain, String authType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        String hostname, String algorithm) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        checkClientTrusted(chain, authType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        checkIdentity(hostname, chain[0], algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Given the partial or complete certificate chain provided by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * peer, check its identity and build a certificate path to a trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * root, return if it can be validated and is trusted for server SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * authentication based on the authentication type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public void checkServerTrusted(X509Certificate[] chain, String authType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        String hostname, String algorithm) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        checkServerTrusted(chain, authType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        checkIdentity(hostname, chain[0], algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // Identify the peer by its certificate and hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private void checkIdentity(String hostname, X509Certificate cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        String algorithm) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (algorithm != null && algorithm.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // if IPv6 strip off the "[]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (hostname != null && hostname.startsWith("[") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                hostname.endsWith("]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                hostname = hostname.substring(1, hostname.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (algorithm.equalsIgnoreCase("HTTPS")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                HostnameChecker.getInstance(HostnameChecker.TYPE_TLS).match(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        hostname, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            } else if (algorithm.equalsIgnoreCase("LDAP")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                HostnameChecker.getInstance(HostnameChecker.TYPE_LDAP).match(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        hostname, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                throw new CertificateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        "Unknown identification algorithm: " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}