jdk/src/share/classes/sun/security/ssl/SSLContextImpl.java
author xuelei
Thu, 13 Nov 2008 23:25:10 -0800
changeset 1580 9af5946d4060
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6745052: SLServerSocket file descriptor leak Summary: SSLServerSocketImpl.checkEnabledSuites() does not release the temporary socket properly Reviewed-by: wetmore, weijun
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 1999-2007 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.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.Socket;
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.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class SSLContextImpl extends SSLContextSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private final EphemeralKeyManager ephemeralKeyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final SSLSessionContextImpl clientCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final SSLSessionContextImpl serverCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private boolean isInitialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private X509ExtendedKeyManager keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private X509TrustManager trustManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private SecureRandom secureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public SSLContextImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    SSLContextImpl(SSLContextImpl other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (other == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            ephemeralKeyManager = new EphemeralKeyManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            clientCache = new SSLSessionContextImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            serverCache = new SSLSessionContextImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            ephemeralKeyManager = other.ephemeralKeyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            clientCache = other.clientCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            serverCache = other.serverCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected void engineInit(KeyManager[] km, TrustManager[] tm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                SecureRandom sr) throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        isInitialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        keyManager = chooseKeyManager(km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (tm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        TrustManagerFactory.getDefaultAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                tmf.init((KeyStore)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                tm = tmf.getTrustManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // eat
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        trustManager = chooseTrustManager(tm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (sr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            secureRandom = JsseJce.getSecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (SunJSSE.isFIPS() && (sr.getProvider() != SunJSSE.cryptoProvider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                throw new KeyManagementException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    ("FIPS mode: SecureRandom must be from provider "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    + SunJSSE.cryptoProvider.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            secureRandom = sr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * The initial delay of seeding the random number generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * could be long enough to cause the initial handshake on our
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * first connection to timeout and fail. Make sure it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * primed and ready by getting some initial output from it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (debug != null && Debug.isOn("sslctx")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            System.out.println("trigger seeding of SecureRandom");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        secureRandom.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (debug != null && Debug.isOn("sslctx")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.out.println("done seeding SecureRandom");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        isInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private X509TrustManager chooseTrustManager(TrustManager[] tm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // We only use the first instance of X509TrustManager passed to us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for (int i = 0; tm != null && i < tm.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (tm[i] instanceof X509TrustManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (SunJSSE.isFIPS() && !(tm[i] instanceof X509TrustManagerImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    throw new KeyManagementException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        ("FIPS mode: only SunJSSE TrustManagers may be used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                return (X509TrustManager)tm[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // nothing found, return a dummy X509TrustManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return DummyX509TrustManager.INSTANCE;
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 X509ExtendedKeyManager chooseKeyManager(KeyManager[] kms)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        for (int i = 0; kms != null && i < kms.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            KeyManager km = kms[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (km instanceof X509KeyManager == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (SunJSSE.isFIPS()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // In FIPS mode, require that one of SunJSSE's own keymanagers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                // is used. Otherwise, we cannot be sure that only keys from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                // the FIPS token are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if ((km instanceof X509KeyManagerImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                            || (km instanceof SunX509KeyManagerImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    return (X509ExtendedKeyManager)km;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    // throw exception, we don't want to silently use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    // dummy keymanager without telling the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    throw new KeyManagementException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        ("FIPS mode: only SunJSSE KeyManagers may be used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (km instanceof X509ExtendedKeyManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return (X509ExtendedKeyManager)km;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (debug != null && Debug.isOn("sslctx")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    "X509KeyManager passed to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    "SSLContext.init():  need an " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    "X509ExtendedKeyManager for SSLEngine use");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return new AbstractWrapper((X509KeyManager)km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // nothing found, return a dummy X509ExtendedKeyManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return DummyX509KeyManager.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected SSLSocketFactory engineGetSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (!isInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                "SSLContextImpl is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return new SSLSocketFactoryImpl(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected SSLServerSocketFactory engineGetServerSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (!isInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new IllegalStateException("SSLContext is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return new SSLServerSocketFactoryImpl(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected SSLEngine engineCreateSSLEngine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (!isInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                "SSLContextImpl is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return new SSLEngineImpl(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected SSLEngine engineCreateSSLEngine(String host, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (!isInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                "SSLContextImpl is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return new SSLEngineImpl(this, host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    protected SSLSessionContext engineGetClientSessionContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return clientCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected SSLSessionContext engineGetServerSessionContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return serverCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    SecureRandom getSecureRandom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return secureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    X509ExtendedKeyManager getX509KeyManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    X509TrustManager getX509TrustManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return trustManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    EphemeralKeyManager getEphemeralKeyManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return ephemeralKeyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
// Dummy X509TrustManager implementation, rejects all peer certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
// Used if the application did not specify a proper X509TrustManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
final class DummyX509TrustManager implements X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static final X509TrustManager INSTANCE = new DummyX509TrustManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private DummyX509TrustManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Given the partial or complete certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * provided by the peer, build a certificate path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * to a trusted root and return if it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * validated and is trusted for client SSL authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * If not, it throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void checkClientTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        throw new CertificateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            "No X509TrustManager implementation avaiable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Given the partial or complete certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * provided by the peer, build a certificate path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * to a trusted root and return if it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * validated and is trusted for server SSL authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * If not, it throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public void checkServerTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        throw new CertificateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            "No X509TrustManager implementation available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Return an array of issuer certificates which are trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * for authenticating peers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return new X509Certificate[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * A wrapper class to turn a X509KeyManager into an X509ExtendedKeyManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
final class AbstractWrapper extends X509ExtendedKeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private final X509KeyManager km;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    AbstractWrapper(X509KeyManager km) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this.km = km;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return km.getClientAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public String chooseClientAlias(String[] keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return km.chooseClientAlias(keyType, issuers, socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public String[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return km.getServerAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public String chooseServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return km.chooseServerAlias(keyType, issuers, socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return km.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return km.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // Inherit chooseEngineClientAlias() and chooseEngineServerAlias() from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    // X509ExtendedKeymanager. It defines them to return null;
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
// Dummy X509KeyManager implementation, never returns any certificates/keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
// Used if the application did not specify a proper X509TrustManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
final class DummyX509KeyManager extends X509ExtendedKeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    static final X509ExtendedKeyManager INSTANCE = new DummyX509KeyManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private DummyX509KeyManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Get the matching aliases for authenticating the client side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Choose an alias to authenticate the client side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public String chooseClientAlias(String[] keyTypes, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Choose an alias to authenticate the client side of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * engine given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public String chooseEngineClientAlias(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            String[] keyTypes, Principal[] issuers, SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Get the matching aliases for authenticating the server side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public String[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Choose an alias to authenticate the server side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public String chooseServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Choose an alias to authenticate the server side of an engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public String chooseEngineServerAlias(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            String keyType, Principal[] issuers, SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * and the root certificate authority last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Returns the key associated with the given alias, using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * password to recover it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @return the requested key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}