jdk/src/share/classes/sun/security/ssl/TrustManagerFactoryImpl.java
author xuelei
Sat, 24 Nov 2012 04:09:19 -0800
changeset 14664 e71aa0962e70
parent 10125 c70d99150c40
child 23010 6dadb192ad81
permissions -rw-r--r--
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl Reviewed-by: xuelei Contributed-by: Florian Weimer <fweimer@redhat.com>
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) 1999, 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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.validator.Validator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
abstract class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private X509TrustManager trustManager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private boolean isInitialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    TrustManagerFactoryImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
    46
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected void engineInit(KeyStore ks) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                ks = getCacertsKeyStore("trustmanager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                // eat security exceptions but report other throwables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                        "SunX509: skip default keystore: " + se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            } catch (Error err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                        "SunX509: skip default keystore: " + err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                throw err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        "SunX509: skip default keystore: " + re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                throw re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        "SunX509: skip default keystore: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                throw new KeyStoreException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    "problem accessing trust store" + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        trustManager = getInstance(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        isInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    abstract X509TrustManager getInstance(KeyStore ks) throws KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    abstract X509TrustManager getInstance(ManagerFactoryParameters spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throws InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
    87
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected void engineInit(ManagerFactoryParameters spec) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        trustManager = getInstance(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        isInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Returns one trust manager for each type of trust material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
    97
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected TrustManager[] engineGetTrustManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (!isInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        "TrustManagerFactoryImpl is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return new TrustManager[] { trustManager };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Try to get an InputStream based on the file we pass in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static FileInputStream getFileInputStream(final File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                new PrivilegedExceptionAction<FileInputStream>() {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   113
                    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    public FileInputStream run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                            if (file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                return new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            // couldn't find it, oh well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the keystore with the configured CA certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static KeyStore getCacertsKeyStore(String dbgname) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String storeFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        File storeFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        FileInputStream fis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        String defaultTrustStoreType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        String defaultTrustStoreProvider;
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   139
        final HashMap<String,String> props = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        final String sep = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        KeyStore ks = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   144
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            public Void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                props.put("trustStore", System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                "javax.net.ssl.trustStore"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                props.put("javaHome", System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                        "java.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                props.put("trustStoreType", System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                "javax.net.ssl.trustStoreType",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                KeyStore.getDefaultType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                props.put("trustStoreProvider", System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                "javax.net.ssl.trustStoreProvider", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                props.put("trustStorePasswd", System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                "javax.net.ssl.trustStorePassword", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                return null;
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
         * Try:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         *      javax.net.ssl.trustStore  (if this variable exists, stop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         *      jssecacerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         *      cacerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * If none exists, we use an empty keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
10125
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   170
        try {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   171
            storeFileName = props.get("trustStore");
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   172
            if (!"NONE".equals(storeFileName)) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   173
                if (storeFileName != null) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   174
                    storeFile = new File(storeFileName);
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   175
                    fis = getFileInputStream(storeFile);
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   176
                } else {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   177
                    String javaHome = props.get("javaHome");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    storeFile = new File(javaHome + sep + "lib" + sep
10125
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   179
                                                    + "security" + sep +
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   180
                                                    "jssecacerts");
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   181
                    if ((fis = getFileInputStream(storeFile)) == null) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   182
                        storeFile = new File(javaHome + sep + "lib" + sep
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   183
                                                    + "security" + sep +
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   184
                                                    "cacerts");
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   185
                        fis = getFileInputStream(storeFile);
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   186
                    }
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   187
                }
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   188
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   189
                if (fis != null) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   190
                    storeFileName = storeFile.getPath();
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   191
                } else {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   192
                    storeFileName = "No File Available, using empty keystore.";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
10125
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   196
            defaultTrustStoreType = props.get("trustStoreType");
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   197
            defaultTrustStoreProvider = props.get("trustStoreProvider");
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   198
            if (debug != null && Debug.isOn(dbgname)) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   199
                System.out.println("trustStore is: " + storeFileName);
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   200
                System.out.println("trustStore type is : " +
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   201
                                    defaultTrustStoreType);
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   202
                System.out.println("trustStore provider is : " +
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   203
                                    defaultTrustStoreProvider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
10125
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   206
            /*
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   207
             * Try to initialize trust store.
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   208
             */
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   209
            if (defaultTrustStoreType.length() != 0) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   210
                if (debug != null && Debug.isOn(dbgname)) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   211
                    System.out.println("init truststore");
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   212
                }
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   213
                if (defaultTrustStoreProvider.length() == 0) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   214
                    ks = KeyStore.getInstance(defaultTrustStoreType);
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   215
                } else {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   216
                    ks = KeyStore.getInstance(defaultTrustStoreType,
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   217
                                            defaultTrustStoreProvider);
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   218
                }
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   219
                char[] passwd = null;
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   220
                String defaultTrustStorePassword =
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   221
                        props.get("trustStorePasswd");
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   222
                if (defaultTrustStorePassword.length() != 0)
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   223
                    passwd = defaultTrustStorePassword.toCharArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
10125
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   225
                // if trustStore is NONE, fis will be null
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   226
                ks.load(fis, passwd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
10125
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   228
                // Zero out the temporary password storage
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   229
                if (passwd != null) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   230
                    for (int i = 0; i < passwd.length; i++) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   231
                        passwd[i] = (char)0;
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   232
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
10125
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   235
        } finally {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   236
            if (fis != null) {
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   237
                fis.close();
c70d99150c40 7059709: close the IO in a final block
xuelei
parents: 9035
diff changeset
   238
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public static final class SimpleFactory extends TrustManagerFactoryImpl {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   245
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        X509TrustManager getInstance(KeyStore ks) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return new X509TrustManagerImpl(Validator.TYPE_SIMPLE, ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   249
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        X509TrustManager getInstance(ManagerFactoryParameters spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                ("SunX509 TrustManagerFactory does not use "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                + "ManagerFactoryParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public static final class PKIXFactory extends TrustManagerFactoryImpl {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   259
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        X509TrustManager getInstance(KeyStore ks) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return new X509TrustManagerImpl(Validator.TYPE_PKIX, ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   263
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        X509TrustManager getInstance(ManagerFactoryParameters spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (spec instanceof CertPathTrustManagerParameters == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    ("Parameters must be CertPathTrustManagerParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            CertPathParameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                ((CertPathTrustManagerParameters)spec).getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (params instanceof PKIXBuilderParameters == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    ("Encapsulated parameters must be PKIXBuilderParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            PKIXBuilderParameters pkixParams = (PKIXBuilderParameters)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return new X509TrustManagerImpl(Validator.TYPE_PKIX, pkixParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
}