jdk/src/share/classes/sun/security/ssl/DefaultSSLContextImpl.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7990 57019dc81b66
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7990
diff changeset
     2
 * Copyright (c) 2005, 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
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * "Default" SSLContext as returned by SSLContext.getDefault(). It comes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * initialized with default KeyManagers and TrustManagers created using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * various system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public final class DefaultSSLContextImpl extends SSLContextImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final String NONE = "NONE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final String P11KEYSTORE = "PKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static volatile SSLContextImpl defaultImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static TrustManager[] defaultTrustManagers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static KeyManager[] defaultKeyManagers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public DefaultSSLContextImpl() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super(defaultImpl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            super.engineInit(getDefaultKeyManager(), getDefaultTrustManager(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            if (debug != null && Debug.isOn("defaultctx")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                System.out.println("default context init failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (defaultImpl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            defaultImpl = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected void engineInit(KeyManager[] km, TrustManager[] tm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            SecureRandom sr) throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throw new KeyManagementException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ("Default SSLContext is initialized automatically");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static synchronized SSLContextImpl getDefaultImpl() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (defaultImpl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            new DefaultSSLContextImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return defaultImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static synchronized TrustManager[] getDefaultTrustManager() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (defaultTrustManagers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return defaultTrustManagers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        KeyStore ks = TrustManagerFactoryImpl.getCacertsKeyStore("defaultctx");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            TrustManagerFactory.getDefaultAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        tmf.init(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        defaultTrustManagers = tmf.getTrustManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return defaultTrustManagers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static synchronized KeyManager[] getDefaultKeyManager() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (defaultKeyManagers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return defaultKeyManagers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 5506
diff changeset
   101
        final Map<String,String> props = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    new PrivilegedExceptionAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            public Object run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                props.put("keyStore",  System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                            "javax.net.ssl.keyStore", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                props.put("keyStoreType", System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                            "javax.net.ssl.keyStoreType",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            KeyStore.getDefaultType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                props.put("keyStoreProvider", System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            "javax.net.ssl.keyStoreProvider", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                props.put("keyStorePasswd", System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            "javax.net.ssl.keyStorePassword", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        final String defaultKeyStore = props.get("keyStore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String defaultKeyStoreType = props.get("keyStoreType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String defaultKeyStoreProvider = props.get("keyStoreProvider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (debug != null && Debug.isOn("defaultctx")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.out.println("keyStore is : " + defaultKeyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            System.out.println("keyStore type is : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                    defaultKeyStoreType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.out.println("keyStore provider is : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                    defaultKeyStoreProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (P11KEYSTORE.equals(defaultKeyStoreType) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                !NONE.equals(defaultKeyStore)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new IllegalArgumentException("if keyStoreType is "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                + P11KEYSTORE + ", then keyStore must be " + NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        FileInputStream fs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (defaultKeyStore.length() != 0 && !NONE.equals(defaultKeyStore)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            fs = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    new PrivilegedExceptionAction<FileInputStream>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                public FileInputStream run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    return new FileInputStream(defaultKeyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        String defaultKeyStorePassword = props.get("keyStorePasswd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        char[] passwd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (defaultKeyStorePassword.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            passwd = defaultKeyStorePassword.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * Try to initialize key store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        KeyStore ks = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if ((defaultKeyStoreType.length()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (debug != null && Debug.isOn("defaultctx")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                System.out.println("init keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (defaultKeyStoreProvider.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                ks = KeyStore.getInstance(defaultKeyStoreType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                ks = KeyStore.getInstance(defaultKeyStoreType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                    defaultKeyStoreProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // if defaultKeyStore is NONE, fs will be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            ks.load(fs, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (fs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            fs.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            fs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * Try to initialize key manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (debug != null && Debug.isOn("defaultctx")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            System.out.println("init keymanager of type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                KeyManagerFactory.getDefaultAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            KeyManagerFactory.getDefaultAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (P11KEYSTORE.equals(defaultKeyStoreType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            kmf.init(ks, null); // do not pass key passwd if using token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            kmf.init(ks, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        defaultKeyManagers = kmf.getKeyManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return defaultKeyManagers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}