src/java.base/share/classes/sun/security/ssl/TrustStoreManager.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47471 304ef03403b1
child 53563 a4b7ea85d668
permissions -rw-r--r--
Initial TLSv1.3 Implementation
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
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
43009
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
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
    28
import java.io.*;
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    29
import java.lang.ref.WeakReference;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    30
import java.security.*;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    31
import java.security.cert.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
    32
import java.util.*;
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    33
import sun.security.action.*;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    34
import sun.security.validator.TrustStoreUtil;
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
/**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    37
 * 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
    38
 * effectively.
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
final class TrustStoreManager {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    41
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    42
    // 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
    43
    private static final TrustAnchorManager tam = new TrustAnchorManager();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    44
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    45
    // Restrict instantiation of this class.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    46
    private TrustStoreManager() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    47
        // empty
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
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    50
    /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    51
     * 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
    52
     * in the default trusted KeyStore.
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
    public static Set<X509Certificate> getTrustedCerts() throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    55
        return tam.getTrustedCerts(TrustStoreDescriptor.createInstance());
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    56
    }
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
    /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    59
     * Return an instance of the default trusted KeyStore.
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
    public static KeyStore getTrustedKeyStore() throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    62
        return tam.getKeyStore(TrustStoreDescriptor.createInstance());
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    63
    }
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
    /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    66
     * A descriptor of the default trusted KeyStore.
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
     * The preference of the default trusted KeyStore is:
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    69
     *    javax.net.ssl.trustStore
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    70
     *    jssecacerts
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    71
     *    cacerts
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    72
     */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    73
    private static final class TrustStoreDescriptor {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    74
        private static final String fileSep = File.separator;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    75
        private static final String defaultStorePath =
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    76
                GetPropertyAction.privilegedGetProperty("java.home") +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    77
                fileSep + "lib" + fileSep + "security";
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    78
        private static final String defaultStore =
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    79
                defaultStorePath + fileSep + "cacerts";
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    80
        private static final String jsseDefaultStore =
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    81
                defaultStorePath + fileSep + "jssecacerts";
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    82
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    83
        // the trust store name
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    84
        private final String storeName;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    85
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    86
        // the trust store type, JKS/PKCS12
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    87
        private final String storeType;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    88
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    89
        // the provider of the trust store
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    90
        private final String storeProvider;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    91
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    92
        // the password used for the trust store
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    93
        private final String storePassword;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    94
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    95
        // the File object of the trust store
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    96
        private final File storeFile;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    97
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    98
        // the last modified time of the store
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
    99
        private final long lastModified;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   100
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   101
        private TrustStoreDescriptor(String storeName, String storeType,
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   102
                String storeProvider, String storePassword,
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   103
                File storeFile, long lastModified) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   104
            this.storeName = storeName;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   105
            this.storeType = storeType;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   106
            this.storeProvider = storeProvider;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   107
            this.storePassword = storePassword;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   108
            this.storeFile = storeFile;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   109
            this.lastModified = lastModified;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   110
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   111
            if (SSLLogger.isOn && SSLLogger.isOn("trustmanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   112
                SSLLogger.fine(
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   113
                    "trustStore is: " + storeName + "\n" +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   114
                    "trustStore type is: " + storeType + "\n" +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   115
                    "trustStore provider is: " + storeProvider + "\n" +
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   116
                    "the last modified time is: " + (new Date(lastModified)));
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   117
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   118
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   119
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   120
        /**
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   121
         * Create an instance of TrustStoreDescriptor for the default
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   122
         * trusted KeyStore.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   123
         */
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   124
        @SuppressWarnings("Convert2Lambda")
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   125
        static TrustStoreDescriptor createInstance() {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   126
             return AccessController.doPrivileged(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   127
                    new PrivilegedAction<TrustStoreDescriptor>() {
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   128
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   129
                @Override
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   130
                public TrustStoreDescriptor run() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   131
                    // Get the system properties for trust store.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   132
                    String storePropName = System.getProperty(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   133
                            "javax.net.ssl.trustStore", jsseDefaultStore);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   134
                    String storePropType = System.getProperty(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   135
                            "javax.net.ssl.trustStoreType",
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   136
                            KeyStore.getDefaultType());
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   137
                    String storePropProvider = System.getProperty(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   138
                            "javax.net.ssl.trustStoreProvider", "");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   139
                    String storePropPassword = System.getProperty(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   140
                            "javax.net.ssl.trustStorePassword", "");
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   141
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   142
                    String temporaryName = "";
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   143
                    File temporaryFile = null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   144
                    long temporaryTime = 0L;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   145
                    if (!"NONE".equals(storePropName)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   146
                        String[] fileNames =
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   147
                                new String[] {storePropName, defaultStore};
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   148
                        for (String fileName : fileNames) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   149
                            File f = new File(fileName);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   150
                            if (f.isFile() && f.canRead()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   151
                                temporaryName = fileName;;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   152
                                temporaryFile = f;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   153
                                temporaryTime = f.lastModified();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   154
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   155
                                break;
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
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   158
                            // Not break, the file is inaccessible.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   159
                            if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   160
                                    SSLLogger.isOn("trustmanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   161
                                SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   162
                                        "Inaccessible trust store: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   163
                                        storePropName);
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   164
                            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   165
                        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   166
                    } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   167
                        temporaryName = storePropName;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   168
                    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   169
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   170
                    return new TrustStoreDescriptor(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   171
                            temporaryName, storePropType, storePropProvider,
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   172
                            storePropPassword, temporaryFile, temporaryTime);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   173
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   174
            });
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
        @Override
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   178
        public boolean equals(Object obj) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   179
            if (obj == this) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   180
                return true;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   181
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   182
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   183
            if (obj instanceof TrustStoreDescriptor) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   184
                TrustStoreDescriptor that = (TrustStoreDescriptor)obj;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   185
                return ((this.lastModified == that.lastModified) &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   186
                    Objects.equals(this.storeName, that.storeName) &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   187
                    Objects.equals(this.storeType, that.storeType) &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   188
                    Objects.equals(this.storeProvider, that.storeProvider));
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   189
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   190
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   191
            return false;
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
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
        // Please be careful if computing security-sensitive attributes'
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   196
        // 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
   197
        @Override
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   198
        public int hashCode() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   199
            int result = 17;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   200
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   201
            if (storeName != null && !storeName.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   202
                result = 31 * result + storeName.hashCode();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   203
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   204
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   205
            if (storeType != null && !storeType.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   206
                result = 31 * result + storeType.hashCode();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   207
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   208
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   209
            if (storeProvider != null && !storeProvider.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   210
                result = 31 * result + storeProvider.hashCode();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   211
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   212
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   213
            if (storeFile != null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   214
                result = 31 * result + storeFile.hashCode();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   215
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   216
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   217
            if (lastModified != 0L) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   218
                result = (int)(31 * result + lastModified);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   219
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   220
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   221
            return result;
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
    }
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
     * 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
   227
     *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   228
     * 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
   229
     * trust KeyStore more effectively.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   230
     */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   231
    private static final class TrustAnchorManager {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   232
        // Last trust store descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   233
        private TrustStoreDescriptor descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   234
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   235
        // The key store used for the trust anchors.
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
        // 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
   238
        // be atomically cleared, and reloaded if needed.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   239
        private WeakReference<KeyStore> ksRef;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   240
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   241
        // 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
   242
        //
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   243
        // 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
   244
        // 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
   245
        private WeakReference<Set<X509Certificate>> csRef;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   246
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   247
        private TrustAnchorManager() {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   248
            this.descriptor = null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   249
            this.ksRef = new WeakReference<>(null);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   250
            this.csRef = new WeakReference<>(null);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   251
        }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   252
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
         * 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
   255
         *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   256
         * @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
   257
         */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   258
        synchronized KeyStore getKeyStore(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   259
                TrustStoreDescriptor descriptor) throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   260
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   261
            TrustStoreDescriptor temporaryDesc = this.descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   262
            KeyStore ks = ksRef.get();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   263
            if ((ks != null) && descriptor.equals(temporaryDesc)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   264
                return ks;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   265
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   266
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   267
            // Reload a new key store.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   268
            if (SSLLogger.isOn && SSLLogger.isOn("trustmanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   269
                SSLLogger.fine("Reload the trust store");
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   270
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   271
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   272
            ks = loadKeyStore(descriptor);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   273
            this.descriptor = descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   274
            this.ksRef = new WeakReference<>(ks);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   275
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   276
            return 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
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
         * 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
   281
         * the specified descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   282
         *
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   283
         * @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
   284
         */
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   285
        synchronized Set<X509Certificate> getTrustedCerts(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   286
                TrustStoreDescriptor descriptor) throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   287
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   288
            KeyStore ks = null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   289
            TrustStoreDescriptor temporaryDesc = this.descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   290
            Set<X509Certificate> certs = csRef.get();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   291
            if (certs != null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   292
                if (descriptor.equals(temporaryDesc)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   293
                    return certs;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   294
                } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   295
                    // Use the new descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   296
                    this.descriptor = descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   297
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   298
            } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   299
                // Try to use the cached store at first.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   300
                if (descriptor.equals(temporaryDesc)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   301
                    ks = ksRef.get();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   302
                } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   303
                    // Use the new descriptor.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   304
                    this.descriptor = descriptor;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   305
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   306
            }
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
            // Reload the trust store if needed.
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   309
            if (ks == null) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   310
                if (SSLLogger.isOn && SSLLogger.isOn("trustmanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   311
                    SSLLogger.fine("Reload the trust store");
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   312
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   313
                ks = loadKeyStore(descriptor);
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
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   316
            // Reload trust certs from the key store.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   317
            if (SSLLogger.isOn && SSLLogger.isOn("trustmanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   318
                SSLLogger.fine("Reload trust certs");
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   319
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   320
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   321
            certs = loadTrustedCerts(ks);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   322
            if (SSLLogger.isOn && SSLLogger.isOn("trustmanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   323
                SSLLogger.fine("Reloaded " + certs.size() + " trust certs");
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   324
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   325
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   326
            // 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
   327
            // 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
   328
            this.csRef = new WeakReference<>(certs);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   329
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   330
            return 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
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   333
        /**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   334
         * Load the the KeyStore as described in the specified descriptor.
43009
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
        private static KeyStore loadKeyStore(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   337
                TrustStoreDescriptor descriptor) throws Exception {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   338
            if (!"NONE".equals(descriptor.storeName) &&
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   339
                    descriptor.storeFile == null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   340
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   341
                // No file available, no KeyStore available.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   342
                if (SSLLogger.isOn && SSLLogger.isOn("trustmanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   343
                    SSLLogger.fine("No available key store");
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   344
                }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   345
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   346
                return null;
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
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   349
            KeyStore ks;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   350
            if (descriptor.storeProvider.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   351
                ks = KeyStore.getInstance(descriptor.storeType);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   352
            } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   353
                ks = KeyStore.getInstance(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   354
                        descriptor.storeType, descriptor.storeProvider);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   355
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   356
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   357
            char[] password = null;
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   358
            if (!descriptor.storePassword.isEmpty()) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   359
                password = descriptor.storePassword.toCharArray();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   360
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   361
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   362
            if (!"NONE".equals(descriptor.storeName)) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   363
                try (FileInputStream fis = AccessController.doPrivileged(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   364
                        new OpenFileInputStreamAction(descriptor.storeFile))) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   365
                    ks.load(fis, password);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   366
                } catch (FileNotFoundException fnfe) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   367
                    // No file available, no KeyStore available.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   368
                    if (SSLLogger.isOn && SSLLogger.isOn("trustmanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47471
diff changeset
   369
                        SSLLogger.fine(
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   370
                            "Not available key store: " + descriptor.storeName);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   371
                    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   372
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   373
                    return null;
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
            } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   376
                ks.load(null, password);
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   377
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   378
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   379
            return ks;
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
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
         * Load trusted certificates from the specified KeyStore.
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
        private static Set<X509Certificate> loadTrustedCerts(KeyStore ks) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   386
            if (ks == null) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   387
                return Collections.<X509Certificate>emptySet();
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   388
            }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   389
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   390
            return TrustStoreUtil.getTrustedCerts(ks);
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
    }
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
diff changeset
   393
}