jdk/src/share/classes/com/sun/net/ssl/SSLSecurity.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 5506 202f599c92aa
permissions -rw-r--r--
7090158: Networking Libraries don't build with javac -Werror Summary: Minor changes to networking java files to remove warnings Reviewed-by: chegar, weijun, hawtin Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2011, 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
 * NOTE:  this file was copied from javax.net.ssl.SSLSecurity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * but was heavily modified to allow com.sun.* users to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * access providers written using the javax.sun.* APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package com.sun.net.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This class instantiates implementations of JSSE engine classes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * providers registered with the java.security.Security object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Jeff Nisewanger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
final class SSLSecurity {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Don't let anyone instantiate this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private SSLSecurity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // ProviderList.getService() is not accessible now, implement our own loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static Service getService(String type, String alg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ProviderList list = Providers.getProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        for (Provider p : list.providers()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            Service s = p.getService(type, alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * The body of the driver for the getImpl method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static Object[] getImpl1(String algName, String engineType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Service service) throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Provider provider = service.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        String className = service.getClassName();
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
    80
        Class<?> implClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            ClassLoader cl = provider.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (cl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                // system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                implClass = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                implClass = cl.loadClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new NoSuchAlgorithmException("Class " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                                " configured for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                                engineType +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                " not found: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new NoSuchAlgorithmException("Class " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                                " configured for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                                engineType +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                                " cannot be accessed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * JSSE 1.0, 1.0.1, and 1.0.2 used the com.sun.net.ssl API as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         * API was being developed.  As JSSE was folded into the main
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * release, it was decided to promote the com.sun.net.ssl API to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * be javax.net.ssl.  It is desired to keep binary compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * with vendors of JSSE implementation written using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * com.sun.net.sll API, so we do this magic to handle everything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         * API used     Implementation used     Supported?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * ========     ===================     ==========
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         * com.sun      javax                   Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * com.sun      com.sun                 Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * javax        javax                   Yes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * javax        com.sun                 Not Currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * Make sure the implementation class is a subclass of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * corresponding engine class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * In wrapping these classes, there's no way to know how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * wrap all possible classes that extend the TrustManager/KeyManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * We only wrap the x509 variants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {   // catch instantiation errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             * (The following Class.forName()s should alway work, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             * this class and all the SPI classes in javax.crypto are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             * loaded by the same class loader.)  That is, unless they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             * give us a SPI class that doesn't exist, say SSLFoo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             * or someone has removed classes from the jsse.jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   136
            Class<?> typeClassJavax;
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   137
            Class<?> typeClassCom;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            Object obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             * Odds are more likely that we have a javax variant, try this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             * first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (((typeClassJavax = Class.forName("javax.net.ssl." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    engineType + "Spi")) != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    (checkSuperclass(implClass, typeClassJavax))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (engineType.equals("SSLContext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    obj = new SSLContextSpiWrapper(algName, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                } else if (engineType.equals("TrustManagerFactory")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    obj = new TrustManagerFactorySpiWrapper(algName, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                } else if (engineType.equals("KeyManagerFactory")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    obj = new KeyManagerFactorySpiWrapper(algName, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                     * We should throw an error if we get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                     * something totally unexpected.  Don't ever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                     * expect to see this one...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        "Class " + implClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        " unknown engineType wrapper:" + engineType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } else if (((typeClassCom = Class.forName("com.sun.net.ssl." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        engineType + "Spi")) != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        (checkSuperclass(implClass, typeClassCom))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                obj = service.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return new Object[] { obj, provider };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new NoSuchAlgorithmException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    "Couldn't locate correct object or wrapper: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    engineType + " " + algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            IllegalStateException exc = new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                "Engine Class Not Found for " + engineType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            exc.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
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
     * Returns an array of objects: the first object in the array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * an instance of an implementation of the requested algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * and type, and the second object in the array identifies the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * of that implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The <code>provName</code> argument can be null, in which case all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * configured providers will be searched in order of preference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static Object[] getImpl(String algName, String engineType, String provName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throws NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        Service service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (provName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            ProviderList list = Providers.getProviderList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            Provider prov = list.getProvider(provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (prov == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throw new NoSuchProviderException("No such provider: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                                  provName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            service = prov.getService(engineType, algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            service = getService(engineType, algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throw new NoSuchAlgorithmException("Algorithm " + algName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                               + " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return getImpl1(algName, engineType, service);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
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
     * Returns an array of objects: the first object in the array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * an instance of an implementation of the requested algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * and type, and the second object in the array identifies the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * of that implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * The <code>prov</code> argument can be null, in which case all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * configured providers will be searched in order of preference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    static Object[] getImpl(String algName, String engineType, Provider prov)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Service service = prov.getService(engineType, algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (service == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new NoSuchAlgorithmException("No such algorithm: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                               algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return getImpl1(algName, engineType, service);
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
     * Checks whether one class is the superclass of another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   240
    private static boolean checkSuperclass(Class<?> subclass, Class<?> superclass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if ((subclass == null) || (superclass == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        while (!subclass.equals(superclass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            subclass = subclass.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (subclass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Return at most the first "resize" elements of an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Didn't want to use java.util.Arrays, as PJava may not have it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static Object[] truncateArray(Object[] oldArray, Object[] newArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        for (int i = 0; i < newArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            newArray[i] = oldArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return newArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * =================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * The remainder of this file is for the wrapper and wrapper-support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * classes.  When SSLSecurity finds something which extends the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * javax.net.ssl.*Spi, we need to go grab a real instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * thing that the Spi supports, and wrap into a com.sun.net.ssl.*Spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 * object.  This also mean that anything going down into the SPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * needs to be wrapped, as well as anything coming back up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
final class SSLContextSpiWrapper extends SSLContextSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private javax.net.ssl.SSLContext theSSLContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    SSLContextSpiWrapper(String algName, Provider prov) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        theSSLContext = javax.net.ssl.SSLContext.getInstance(algName, prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    protected void engineInit(KeyManager[] kma, TrustManager[] tma,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            SecureRandom sr) throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // Keep track of the actual number of array elements copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        javax.net.ssl.KeyManager[] kmaw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        javax.net.ssl.TrustManager[] tmaw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // Convert com.sun.net.ssl.kma to a javax.net.ssl.kma
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // wrapper if need be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (kma != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            kmaw = new javax.net.ssl.KeyManager[kma.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            for (src = 0, dst = 0; src < kma.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                 * These key managers may implement both javax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                 * and com.sun interfaces, so if they do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                 * javax, there's no need to wrap them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (!(kma[src] instanceof javax.net.ssl.KeyManager)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                     * Do we know how to convert them?  If not, oh well...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                     * We'll have to drop them on the floor in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                     * case, cause we don't know how to handle them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                     * This will be pretty rare, but put here for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                     * completeness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    if (kma[src] instanceof X509KeyManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        kmaw[dst] = (javax.net.ssl.KeyManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                            new X509KeyManagerJavaxWrapper(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                            (X509KeyManager)kma[src]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        dst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    // We can convert directly, since they implement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    kmaw[dst] = (javax.net.ssl.KeyManager)kma[src];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    dst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
             * If dst != src, there were more items in the original array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
             * than in the new array.  Compress the new elements to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
             * any problems down the road.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (dst != src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    kmaw = (javax.net.ssl.KeyManager [])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        SSLSecurity.truncateArray(kmaw,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            new javax.net.ssl.KeyManager [dst]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            kmaw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Now do the same thing with the TrustManagers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (tma != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            tmaw = new javax.net.ssl.TrustManager[tma.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            for (src = 0, dst = 0; src < tma.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                 * These key managers may implement both...see above...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (!(tma[src] instanceof javax.net.ssl.TrustManager)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    // Do we know how to convert them?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    if (tma[src] instanceof X509TrustManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        tmaw[dst] = (javax.net.ssl.TrustManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            new X509TrustManagerJavaxWrapper(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                            (X509TrustManager)tma[src]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        dst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    tmaw[dst] = (javax.net.ssl.TrustManager)tma[src];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    dst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (dst != src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                tmaw = (javax.net.ssl.TrustManager [])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    SSLSecurity.truncateArray(tmaw,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        new javax.net.ssl.TrustManager [dst]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            tmaw = 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
        theSSLContext.init(kmaw, tmaw, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    protected javax.net.ssl.SSLSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            engineGetSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return theSSLContext.getSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    protected javax.net.ssl.SSLServerSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            engineGetServerSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return theSSLContext.getServerSocketFactory();
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
final class TrustManagerFactorySpiWrapper extends TrustManagerFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private javax.net.ssl.TrustManagerFactory theTrustManagerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    TrustManagerFactorySpiWrapper(String algName, Provider prov) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        theTrustManagerFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            javax.net.ssl.TrustManagerFactory.getInstance(algName, prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    protected void engineInit(KeyStore ks) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        theTrustManagerFactory.init(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    protected TrustManager[] engineGetTrustManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        int dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        int src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        javax.net.ssl.TrustManager[] tma =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            theTrustManagerFactory.getTrustManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        TrustManager[] tmaw = new TrustManager[tma.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        for (src = 0, dst = 0; src < tma.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (!(tma[src] instanceof com.sun.net.ssl.TrustManager)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // We only know how to wrap X509TrustManagers, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                // TrustManagers don't have any methods to wrap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                if (tma[src] instanceof javax.net.ssl.X509TrustManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    tmaw[dst] = (TrustManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        new X509TrustManagerComSunWrapper(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        (javax.net.ssl.X509TrustManager)tma[src]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    dst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                tmaw[dst] = (TrustManager)tma[src];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                dst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (dst != src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            tmaw = (TrustManager [])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                SSLSecurity.truncateArray(tmaw, new TrustManager [dst]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return tmaw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
final class KeyManagerFactorySpiWrapper extends KeyManagerFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    private javax.net.ssl.KeyManagerFactory theKeyManagerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    KeyManagerFactorySpiWrapper(String algName, Provider prov) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        theKeyManagerFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            javax.net.ssl.KeyManagerFactory.getInstance(algName, prov);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    protected void engineInit(KeyStore ks, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throws KeyStoreException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            UnrecoverableKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        theKeyManagerFactory.init(ks, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    protected KeyManager[] engineGetKeyManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        int src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        javax.net.ssl.KeyManager[] kma =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            theKeyManagerFactory.getKeyManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        KeyManager[] kmaw = new KeyManager[kma.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        for (src = 0, dst = 0; src < kma.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (!(kma[src] instanceof com.sun.net.ssl.KeyManager)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // We only know how to wrap X509KeyManagers, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                // KeyManagers don't have any methods to wrap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                if (kma[src] instanceof javax.net.ssl.X509KeyManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    kmaw[dst] = (KeyManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        new X509KeyManagerComSunWrapper(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        (javax.net.ssl.X509KeyManager)kma[src]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    dst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                kmaw[dst] = (KeyManager)kma[src];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                dst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            src++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (dst != src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            kmaw = (KeyManager [])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                SSLSecurity.truncateArray(kmaw, new KeyManager [dst]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return kmaw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
// =================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
final class X509KeyManagerJavaxWrapper implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        javax.net.ssl.X509KeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    private X509KeyManager theX509KeyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    X509KeyManagerJavaxWrapper(X509KeyManager obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        theX509KeyManager = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return theX509KeyManager.getClientAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public String chooseClientAlias(String[] keyTypes, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        String retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (keyTypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * Scan the list, look for something we can pass back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        for (int i = 0; i < keyTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if ((retval = theX509KeyManager.chooseClientAlias(keyTypes[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    issuers)) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * JSSE 1.0.x was only socket based, but it's possible someone might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * want to install a really old provider.  We should at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * try to be nice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public String chooseEngineClientAlias(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            String[] keyTypes, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            javax.net.ssl.SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        String retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (keyTypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         * Scan the list, look for something we can pass back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        for (int i = 0; i < keyTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if ((retval = theX509KeyManager.chooseClientAlias(keyTypes[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    issuers)) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public String[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return theX509KeyManager.getServerAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public String chooseServerAlias(String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (keyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return theX509KeyManager.chooseServerAlias(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * JSSE 1.0.x was only socket based, but it's possible someone might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * want to install a really old provider.  We should at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * try to be nice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public String chooseEngineServerAlias(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            String keyType, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            javax.net.ssl.SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (keyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        return theX509KeyManager.chooseServerAlias(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public java.security.cert.X509Certificate[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        return theX509KeyManager.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return theX509KeyManager.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
final class X509TrustManagerJavaxWrapper implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        javax.net.ssl.X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    private X509TrustManager theX509TrustManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    X509TrustManagerJavaxWrapper(X509TrustManager obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        theX509TrustManager = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public void checkClientTrusted(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            java.security.cert.X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        throws java.security.cert.CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (!theX509TrustManager.isClientTrusted(chain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            throw new java.security.cert.CertificateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                "Untrusted Client Certificate Chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public void checkServerTrusted(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            java.security.cert.X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        throws java.security.cert.CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (!theX509TrustManager.isServerTrusted(chain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            throw new java.security.cert.CertificateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                "Untrusted Server Certificate Chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return theX509TrustManager.getAcceptedIssuers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
final class X509KeyManagerComSunWrapper implements X509KeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private javax.net.ssl.X509KeyManager theX509KeyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    X509KeyManagerComSunWrapper(javax.net.ssl.X509KeyManager obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        theX509KeyManager = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return theX509KeyManager.getClientAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public String chooseClientAlias(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        String [] keyTypes = new String [] { keyType };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return theX509KeyManager.chooseClientAlias(keyTypes, issuers, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    public String[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return theX509KeyManager.getServerAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    public String chooseServerAlias(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return theX509KeyManager.chooseServerAlias(keyType, issuers, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public java.security.cert.X509Certificate[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return theX509KeyManager.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        return theX509KeyManager.getPrivateKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
final class X509TrustManagerComSunWrapper implements X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    private javax.net.ssl.X509TrustManager theX509TrustManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    X509TrustManagerComSunWrapper(javax.net.ssl.X509TrustManager obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        theX509TrustManager = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    public boolean isClientTrusted(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            java.security.cert.X509Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            theX509TrustManager.checkClientTrusted(chain, "UNKNOWN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        } catch (java.security.cert.CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    public boolean isServerTrusted(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            java.security.cert.X509Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            theX509TrustManager.checkServerTrusted(chain, "UNKNOWN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        } catch (java.security.cert.CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        return theX509TrustManager.getAcceptedIssuers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
}