jdk/src/java.base/share/classes/sun/security/ssl/TrustStoreManager.java
author xuelei
Fri, 06 Jan 2017 02:03:47 +0000
changeset 43009 5af9f7aa93e5
permissions -rw-r--r--
8129988: JSSE should create a single instance of the cacerts KeyStore Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     1
/*
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     4
 *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    10
 *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    15
 * accompanied this code).
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    16
 *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    20
 *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    23
 * questions.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    24
 */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    25
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    26
package sun.security.ssl;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    27
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    28
import java.lang.ref.WeakReference;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    29
import java.io.*;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    30
import java.util.*;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    31
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    32
import java.security.*;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    33
import java.security.cert.*;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    34
import java.security.cert.Certificate;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    35
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    36
import sun.security.action.*;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    37
import sun.security.validator.TrustStoreUtil;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    38
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    39
/**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    40
 * Collection of static utility methods to manage the default trusted KeyStores
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    41
 * effectively.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    42
 */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    43
final class TrustStoreManager {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    44
    private static final Debug debug = Debug.getInstance("ssl");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    45
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    46
    // A singleton service to manage the default trusted KeyStores effectively.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    47
    private static final TrustAnchorManager tam = new TrustAnchorManager();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    48
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    49
    // Restrict instantiation of this class.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    50
    private TrustStoreManager() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    51
        // empty
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    52
    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    53
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    54
    /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    55
     * Return an unmodifiable set of all trusted X509Certificates contained
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    56
     * in the default trusted KeyStore.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    57
     */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    58
    public static Set<X509Certificate> getTrustedCerts() throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    59
        return tam.getTrustedCerts(TrustStoreDescriptor.createInstance());
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    60
    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    61
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    62
    /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    63
     * Return an instance of the default trusted KeyStore.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    64
     */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    65
    public static KeyStore getTrustedKeyStore() throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    66
        return tam.getKeyStore(TrustStoreDescriptor.createInstance());
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    67
    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    68
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    69
    /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    70
     * A descriptor of the default trusted KeyStore.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    71
     *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    72
     * The preference of the default trusted KeyStore is:
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    73
     *    javax.net.ssl.trustStore
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    74
     *    jssecacerts
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    75
     *    cacerts
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    76
     */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    77
    private static final class TrustStoreDescriptor {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    78
        private static final String fileSep = File.separator;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    79
        private static final String defaultStorePath =
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    80
                GetPropertyAction.privilegedGetProperty("java.home") +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    81
                fileSep + "lib" + fileSep + "security";
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    82
        private static final String defaultStore =
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    83
                defaultStorePath + fileSep + "cacerts";
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    84
        private static final String jsseDefaultStore =
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    85
                defaultStorePath + fileSep + "jssecacerts";
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    86
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    87
        // the trust store name
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    88
        private final String storeName;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    89
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    90
        // the trust store type, JKS/PKCS12
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    91
        private final String storeType;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    92
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    93
        // the provider of the trust store
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    94
        private final String storeProvider;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    95
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    96
        // the password used for the trust store
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    97
        private final String storePassword;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    98
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    99
        // the File object of the trust store
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   100
        private final File storeFile;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   101
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   102
        // the last modified time of the store
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   103
        private final long lastModified;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   104
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   105
        private TrustStoreDescriptor(String storeName, String storeType,
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   106
                String storeProvider, String storePassword,
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   107
                File storeFile, long lastModified) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   108
            this.storeName = storeName;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   109
            this.storeType = storeType;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   110
            this.storeProvider = storeProvider;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   111
            this.storePassword = storePassword;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   112
            this.storeFile = storeFile;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   113
            this.lastModified = lastModified;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   114
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   115
            if (debug != null && Debug.isOn("trustmanager")) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   116
                System.out.println(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   117
                    "trustStore is: " + storeName + "\n" +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   118
                    "trustStore type is: " + storeType + "\n" +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   119
                    "trustStore provider is: " + storeProvider + "\n" +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   120
                    "the last modified time is: " + (new Date(lastModified)));
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   121
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   122
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   123
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   124
        /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   125
         * Create an instance of TrustStoreDescriptor for the default
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   126
         * trusted KeyStore.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   127
         */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   128
        static TrustStoreDescriptor createInstance() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   129
             return AccessController.doPrivileged(new PrivilegedAction<>() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   130
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   131
                @Override
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   132
                public TrustStoreDescriptor run() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   133
                    // Get the system properties for trust store.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   134
                    String storePropName = System.getProperty(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   135
                            "javax.net.ssl.trustStore", jsseDefaultStore);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   136
                    String storePropType = System.getProperty(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   137
                            "javax.net.ssl.trustStoreType",
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   138
                            KeyStore.getDefaultType());
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   139
                    String storePropProvider = System.getProperty(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   140
                            "javax.net.ssl.trustStoreProvider", "");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   141
                    String storePropPassword = System.getProperty(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   142
                            "javax.net.ssl.trustStorePassword", "");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   143
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   144
                    String temporaryName = "";
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   145
                    File temporaryFile = null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   146
                    long temporaryTime = 0L;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   147
                    if (!"NONE".equals(storePropName)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   148
                        String[] fileNames =
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   149
                                new String[] {storePropName, defaultStore};
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   150
                        for (String fileName : fileNames) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   151
                            File f = new File(fileName);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   152
                            if (f.isFile() && f.canRead()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   153
                                temporaryName = fileName;;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   154
                                temporaryFile = f;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   155
                                temporaryTime = f.lastModified();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   156
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   157
                                break;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   158
                            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   159
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   160
                            // Not break, the file is inaccessible.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   161
                            if (debug != null &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   162
                                    Debug.isOn("trustmanager")) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   163
                                System.out.println(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   164
                                    "Inaccessible trust store: " +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   165
                                    storePropName);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   166
                            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   167
                        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   168
                    } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   169
                        temporaryName = storePropName;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   170
                    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   171
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   172
                    return new TrustStoreDescriptor(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   173
                            temporaryName, storePropType, storePropProvider,
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   174
                            storePropPassword, temporaryFile, temporaryTime);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   175
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   176
            });
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   177
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   178
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   179
        @Override
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   180
        public boolean equals(Object obj) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   181
            if (obj == this) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   182
                return true;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   183
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   184
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   185
            if (obj instanceof TrustStoreDescriptor) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   186
                TrustStoreDescriptor that = (TrustStoreDescriptor)obj;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   187
                return ((this.lastModified == that.lastModified) &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   188
                    Objects.equals(this.storeName, that.storeName) &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   189
                    Objects.equals(this.storeType, that.storeType) &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   190
                    Objects.equals(this.storeProvider, that.storeProvider));
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   191
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   192
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   193
            return false;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   194
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   195
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   196
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   197
        // Please be careful if computing security-sensitive attributes'
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   198
        // hash code.  For example the storePassword should not be computed.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   199
        @Override
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   200
        public int hashCode() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   201
            int result = 17;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   202
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   203
            if (storeName != null && !storeName.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   204
                result = 31 * result + storeName.hashCode();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   205
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   206
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   207
            if (storeType != null && !storeType.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   208
                result = 31 * result + storeType.hashCode();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   209
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   210
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   211
            if (storeProvider != null && !storeProvider.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   212
                result = 31 * result + storeProvider.hashCode();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   213
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   214
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   215
            if (storeFile != null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   216
                result = 31 * result + storeFile.hashCode();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   217
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   218
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   219
            if (lastModified != 0L) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   220
                result = (int)(31 * result + lastModified);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   221
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   222
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   223
            return result;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   224
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   225
    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   226
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   227
    /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   228
     * The trust anchors manager used to expedite the performance.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   229
     *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   230
     * This class can be used to provide singleton services to access default
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   231
     * trust KeyStore more effectively.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   232
     */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   233
    private static final class TrustAnchorManager {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   234
        // Last trust store descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   235
        private TrustStoreDescriptor descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   236
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   237
        // The key store used for the trust anchors.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   238
        //
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   239
        // Use weak reference so that the heavy loaded KeyStore object can
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   240
        // be atomically cleared, and reloaded if needed.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   241
        private WeakReference<KeyStore> ksRef;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   242
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   243
        // The trusted X.509 certificates in the key store.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   244
        //
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   245
        // Use weak reference so that the heavy loaded certificates collection
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   246
        // objects can be atomically cleared, and reloaded if needed.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   247
        private WeakReference<Set<X509Certificate>> csRef;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   248
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   249
        private TrustAnchorManager() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   250
            this.descriptor = null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   251
            this.ksRef = new WeakReference<>(null);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   252
            this.csRef = new WeakReference<>(null);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   253
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   254
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   255
        /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   256
         * Get the default trusted KeyStore with the specified descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   257
         *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   258
         * @return null if the underlying KeyStore is not available.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   259
         */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   260
        synchronized KeyStore getKeyStore(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   261
                TrustStoreDescriptor descriptor) throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   262
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   263
            TrustStoreDescriptor temporaryDesc = this.descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   264
            KeyStore ks = ksRef.get();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   265
            if ((ks != null) && descriptor.equals(temporaryDesc)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   266
                return ks;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   267
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   268
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   269
            // Reload a new key store.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   270
            if ((debug != null) && Debug.isOn("trustmanager")) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   271
                System.out.println("Reload the trust store");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   272
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   273
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   274
            ks = loadKeyStore(descriptor);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   275
            this.descriptor = descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   276
            this.ksRef = new WeakReference<>(ks);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   277
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   278
            return ks;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   279
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   280
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   281
        /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   282
         * Get trusted certificates in the default trusted KeyStore with
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   283
         * the specified descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   284
         *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   285
         * @return empty collection if the underlying KeyStore is not available.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   286
         */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   287
        synchronized Set<X509Certificate> getTrustedCerts(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   288
                TrustStoreDescriptor descriptor) throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   289
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   290
            KeyStore ks = null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   291
            TrustStoreDescriptor temporaryDesc = this.descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   292
            Set<X509Certificate> certs = csRef.get();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   293
            if (certs != null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   294
                if (descriptor.equals(temporaryDesc)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   295
                    return certs;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   296
                } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   297
                    // Use the new descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   298
                    this.descriptor = descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   299
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   300
            } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   301
                // Try to use the cached store at first.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   302
                if (descriptor.equals(temporaryDesc)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   303
                    ks = ksRef.get();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   304
                } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   305
                    // Use the new descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   306
                    this.descriptor = descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   307
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   308
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   309
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   310
            // Reload the trust store if needed.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   311
            if (ks == null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   312
                if ((debug != null) && Debug.isOn("trustmanager")) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   313
                    System.out.println("Reload the trust store");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   314
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   315
                ks = loadKeyStore(descriptor);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   316
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   317
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   318
            // Reload trust certs from the key store.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   319
            if ((debug != null) && Debug.isOn("trustmanager")) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   320
                System.out.println("Reload trust certs");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   321
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   322
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   323
            certs = loadTrustedCerts(ks);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   324
            if ((debug != null) && Debug.isOn("trustmanager")) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   325
                System.out.println("Reloaded " + certs.size() + " trust certs");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   326
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   327
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   328
            // Note that as ks is a local variable, it is not
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   329
            // necessary to add it to the ksRef weak reference.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   330
            this.csRef = new WeakReference<>(certs);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   331
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   332
            return certs;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   333
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   334
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   335
        /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   336
         * Load the the KeyStore as described in the specified descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   337
         */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   338
        private static KeyStore loadKeyStore(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   339
                TrustStoreDescriptor descriptor) throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   340
            if (!"NONE".equals(descriptor.storeName) &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   341
                    descriptor.storeFile == null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   342
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   343
                // No file available, no KeyStore available.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   344
                if (debug != null && Debug.isOn("trustmanager")) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   345
                    System.out.println("No available key store");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   346
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   347
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   348
                return null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   349
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   350
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   351
            KeyStore ks;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   352
            if (descriptor.storeProvider.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   353
                ks = KeyStore.getInstance(descriptor.storeType);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   354
            } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   355
                ks = KeyStore.getInstance(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   356
                        descriptor.storeType, descriptor.storeProvider);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   357
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   358
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   359
            char[] password = null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   360
            if (!descriptor.storePassword.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   361
                password = descriptor.storePassword.toCharArray();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   362
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   363
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   364
            if (!"NONE".equals(descriptor.storeName)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   365
                try (FileInputStream fis = AccessController.doPrivileged(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   366
                        new OpenFileInputStreamAction(descriptor.storeFile))) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   367
                    ks.load(fis, password);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   368
                } catch (FileNotFoundException fnfe) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   369
                    // No file available, no KeyStore available.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   370
                    if (debug != null && Debug.isOn("trustmanager")) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   371
                        System.out.println(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   372
                            "Not available key store: " + descriptor.storeName);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   373
                    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   374
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   375
                    return null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   376
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   377
            } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   378
                ks.load(null, password);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   379
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   380
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   381
            return ks;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   382
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   383
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   384
        /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   385
         * Load trusted certificates from the specified KeyStore.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   386
         */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   387
        private static Set<X509Certificate> loadTrustedCerts(KeyStore ks) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   388
            if (ks == null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   389
                return Collections.<X509Certificate>emptySet();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   390
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   391
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   392
            return TrustStoreUtil.getTrustedCerts(ks);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   393
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   394
    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   395
}