jdk/src/java.base/share/classes/sun/security/ssl/TrustManagerFactoryImpl.java
author ascarpino
Wed, 08 Feb 2017 12:08:28 -0800
changeset 43701 fe8c324ba97c
parent 43009 5af9f7aa93e5
permissions -rw-r--r--
8160655: Fix denyAfter and usage types for security properties Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14664
diff changeset
     2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.validator.Validator;
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
    35
import sun.security.validator.TrustStoreUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
abstract class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private X509TrustManager trustManager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private boolean isInitialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    TrustManagerFactoryImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
    47
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    protected void engineInit(KeyStore ks) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            try {
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
    51
                trustManager = getInstance(TrustStoreManager.getTrustedCerts());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                // eat security exceptions but report other throwables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                        "SunX509: skip default keystore: " + se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            } catch (Error err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                        "SunX509: skip default keystore: " + err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                throw err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        "SunX509: skip default keystore: " + re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                throw re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (debug != null && Debug.isOn("trustmanager")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        "SunX509: skip default keystore: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throw new KeyStoreException(
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
    76
                    "problem accessing trust store", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
    78
        } else {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
    79
            trustManager = getInstance(TrustStoreUtil.getTrustedCerts(ks));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
    81
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        isInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
    85
    abstract X509TrustManager getInstance(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
    86
            Collection<X509Certificate> trustedCerts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    abstract X509TrustManager getInstance(ManagerFactoryParameters spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throws InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
    91
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected void engineInit(ManagerFactoryParameters spec) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        trustManager = getInstance(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        isInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Returns one trust manager for each type of trust material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   101
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected TrustManager[] engineGetTrustManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (!isInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        "TrustManagerFactoryImpl is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return new TrustManager[] { trustManager };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Try to get an InputStream based on the file we pass in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static FileInputStream getFileInputStream(final File file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                new PrivilegedExceptionAction<FileInputStream>() {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   117
                    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    public FileInputStream run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                            if (file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                return new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            // couldn't find it, oh well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   133
    public static final class SimpleFactory extends TrustManagerFactoryImpl {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   134
        @Override
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   135
        X509TrustManager getInstance(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   136
                Collection<X509Certificate> trustedCerts) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   137
            return new X509TrustManagerImpl(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   138
                    Validator.TYPE_SIMPLE, trustedCerts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   141
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        X509TrustManager getInstance(ManagerFactoryParameters spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                ("SunX509 TrustManagerFactory does not use "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                + "ManagerFactoryParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   148
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public static final class PKIXFactory extends TrustManagerFactoryImpl {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   151
        @Override
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   152
        X509TrustManager getInstance(
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   153
                Collection<X509Certificate> trustedCerts) {
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   154
            return new X509TrustManagerImpl(Validator.TYPE_PKIX, trustedCerts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
43009
5af9f7aa93e5 8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents: 25859
diff changeset
   156
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10125
diff changeset
   157
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        X509TrustManager getInstance(ManagerFactoryParameters spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (spec instanceof CertPathTrustManagerParameters == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    ("Parameters must be CertPathTrustManagerParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            CertPathParameters params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                ((CertPathTrustManagerParameters)spec).getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (params instanceof PKIXBuilderParameters == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    ("Encapsulated parameters must be PKIXBuilderParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            PKIXBuilderParameters pkixParams = (PKIXBuilderParameters)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return new X509TrustManagerImpl(Validator.TYPE_PKIX, pkixParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}