jdk/src/share/classes/sun/security/ssl/SunX509KeyManagerImpl.java
author smarks
Fri, 14 Jan 2011 15:31:45 -0800
changeset 7990 57019dc81b66
parent 5506 202f599c92aa
child 9035 1255eb81cc2f
permissions -rw-r--r--
7012003: diamond conversion for ssl Reviewed-by: wetmore
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: 715
diff changeset
     2
 * Copyright (c) 1999, 2008, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * An implemention of X509KeyManager backed by a KeyStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The backing KeyStore is inspected when this object is constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * All key entries containing a PrivateKey and a non-empty chain of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * X509Certificate are then copied into an internal store. This means
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * that subsequent modifications of the KeyStore have no effect on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * X509KeyManagerImpl object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Note that this class assumes that all keys are protected by the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The JSSE handshake code currently calls into this class via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * chooseClientAlias() and chooseServerAlias() to find the certificates to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * use. As implemented here, both always return the first alias returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * getClientAliases() and getServerAliases(). In turn, these methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * implemented by calling getAliases(), which performs the actual lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Note that this class currently implements no checking of the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * certificates. In particular, it is *not* guaranteed that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *  . the certificates are within their validity period and not revoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *  . the signatures verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *  . they form a PKIX compliant chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *  . the certificate extensions allow the certificate to be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *    the desired purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Chains that fail any of these criteria will probably be rejected by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
final class SunX509KeyManagerImpl extends X509ExtendedKeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final String[] STRING0 = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The credentials from the KeyStore as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Map: String(alias) -> X509Credentials(credentials)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Map<String,X509Credentials> credentialsMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Cached server aliases for the case issuers == null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * (in the current JSSE implementation, issuers are always null for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * server certs). See chooseServerAlias() for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Map: String(keyType) -> String[](alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private Map<String,String[]> serverAliasCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Basic container for credentials implemented as an inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static class X509Credentials {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        X509Certificate[] certificates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        private Set<X500Principal> issuerX500Principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        X509Credentials(PrivateKey privateKey, X509Certificate[] certificates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // assert privateKey and certificates != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            this.privateKey = privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            this.certificates = certificates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        synchronized Set<X500Principal> getIssuerX500Principals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // lazy initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (issuerX500Principals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                issuerX500Principals = new HashSet<X500Principal>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                for (int i = 0; i < certificates.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    issuerX500Principals.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                certificates[i].getIssuerX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return issuerX500Principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    SunX509KeyManagerImpl(KeyStore ks, char[] password) throws KeyStoreException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            NoSuchAlgorithmException, UnrecoverableKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        credentialsMap = new HashMap<String,X509Credentials>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        serverAliasCache = new HashMap<String,String[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        for (Enumeration<String> aliases = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                        aliases.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            String alias = aliases.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (!ks.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Key key = ks.getKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (key instanceof PrivateKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            Certificate[] certs = ks.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if ((certs == null) || (certs.length == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    !(certs[0] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (!(certs instanceof X509Certificate[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                Certificate[] tmp = new X509Certificate[certs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.arraycopy(certs, 0, tmp, 0, certs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                certs = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            X509Credentials cred = new X509Credentials((PrivateKey)key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                (X509Certificate[])certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            credentialsMap.put(alias, cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (debug != null && Debug.isOn("keymanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                System.out.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                System.out.println("found key for : " + alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    System.out.println("chain [" + i + "] = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    + certs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                System.out.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * and the root certificate authority last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        X509Credentials cred = credentialsMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (cred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   175
            return cred.certificates.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns the key associated with the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        X509Credentials cred = credentialsMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (cred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return cred.privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
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
     * Choose an alias to authenticate the client side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public String chooseClientAlias(String[] keyTypes, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * We currently don't do anything with socket, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * someday we might.  It might be a useful hint for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * selecting one of the aliases we get back from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         * getClientAliases().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (keyTypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        for (int i = 0; i < keyTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            String[] aliases = getClientAliases(keyTypes[i], issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if ((aliases != null) && (aliases.length > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                return aliases[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Choose an alias to authenticate the client side of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <code>SSLEngine</code> connection given the public key type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * and the list of certificate issuer authorities recognized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public String chooseEngineClientAlias(String[] keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            Principal[] issuers, SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * If we ever start using socket as a selection criteria,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * we'll need to adjust this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return chooseClientAlias(keyType, issuers, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Choose an alias to authenticate the server side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public String chooseServerAlias(String keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            Principal[] issuers, Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * We currently don't do anything with socket, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * someday we might.  It might be a useful hint for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * selecting one of the aliases we get back from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * getServerAliases().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (keyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        String[] aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (issuers == null || issuers.length == 0) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   258
            aliases = serverAliasCache.get(keyType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (aliases == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                aliases = getServerAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                // Cache the result (positive and negative lookups)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (aliases == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    aliases = STRING0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                serverAliasCache.put(keyType, aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            aliases = getServerAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if ((aliases != null) && (aliases.length > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return aliases[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Choose an alias to authenticate the server side of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <code>SSLEngine</code> connection given the public key type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * and the list of certificate issuer authorities recognized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public String chooseEngineServerAlias(String keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Principal[] issuers, SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * If we ever start using socket as a selection criteria,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * we'll need to adjust this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return chooseServerAlias(keyType, issuers, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Get the matching aliases for authenticating the client side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return getAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Get the matching aliases for authenticating the server side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public String[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return getAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Get the matching aliases for authenticating the either side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Issuers comes to us in the form of X500Principal[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private String[] getAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (keyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (issuers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            issuers = new X500Principal[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (issuers instanceof X500Principal[] == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            // normally, this will never happen but try to recover if it does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            issuers = convertPrincipals(issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        String sigType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (keyType.contains("_")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            int k = keyType.indexOf("_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            sigType = keyType.substring(k + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            keyType = keyType.substring(0, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            sigType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        X500Principal[] x500Issuers = (X500Principal[])issuers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // the algorithm below does not produce duplicates, so avoid Set
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 5506
diff changeset
   340
        List<String> aliases = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        for (Map.Entry<String,X509Credentials> entry :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                                credentialsMap.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            String alias = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            X509Credentials credentials = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            X509Certificate[] certs = credentials.certificates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (!keyType.equals(certs[0].getPublicKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (sigType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (certs.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    // if possible, check the public key in the issuer cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    if (!sigType.equals(certs[1].getPublicKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    // Check the signature algorithm of the certificate itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    // Look for the "withRSA" in "SHA1withRSA", etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    String sigAlgName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                            certs[0].getSigAlgName().toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    String pattern = "WITH" + sigType.toUpperCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    if (sigAlgName.contains(pattern) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (issuers.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                // no issuer specified, match all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                aliases.add(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                if (debug != null && Debug.isOn("keymanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    System.out.println("matching alias: " + alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                Set<X500Principal> certIssuers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                        credentials.getIssuerX500Principals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                for (int i = 0; i < x500Issuers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    if (certIssuers.contains(issuers[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        aliases.add(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        if (debug != null && Debug.isOn("keymanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            System.out.println("matching alias: " + alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   391
        String[] aliasStrings = aliases.toArray(STRING0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return ((aliasStrings.length == 0) ? null : aliasStrings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Convert an array of Principals to an array of X500Principals, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * possible. Principals that cannot be converted are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private static X500Principal[] convertPrincipals(Principal[] principals) {
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 5506
diff changeset
   400
        List<X500Principal> list = new ArrayList<>(principals.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        for (int i = 0; i < principals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            Principal p = principals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (p instanceof X500Principal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                list.add((X500Principal)p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    list.add(new X500Principal(p.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return list.toArray(new X500Principal[list.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
}