jdk/src/java.base/share/classes/com/sun/net/ssl/TrustManagerFactory.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 30033 b9c86c17164a
child 39562 672b948cb355
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * NOTE:  this file was copied from javax.net.ssl.TrustManagerFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package com.sun.net.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class acts as a factory for trust managers based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * source of trust material. Each trust manager manages a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * type of trust material for use by secure sockets. The trust
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * material is based on a KeyStore and/or provider specific sources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @deprecated As of JDK 1.4, this implementation-specific class was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *      replaced by {@link javax.net.ssl.TrustManagerFactory}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class TrustManagerFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private TrustManagerFactorySpi factorySpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // The name of the trust management algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <p>The default TrustManager can be changed by setting the value of the
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    56
     * {@code sun.ssl.trustmanager.type} security property to the desired name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    58
     * @return the default type as specified by the
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    59
     * {@code sun.ssl.trustmanager.type} security property, or an
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    60
     * implementation-specific default if no such property exists.
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    61
     *
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    62
     * @see java.security.Security security properties
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30033
diff changeset
    64
    public static final String getDefaultAlgorithm() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String type;
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    66
        type = AccessController.doPrivileged(new PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                return Security.getProperty("sun.ssl.trustmanager.type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            type = "SunX509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates a TrustManagerFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param factorySpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected TrustManagerFactory(TrustManagerFactorySpi factorySpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Provider provider, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.factorySpi = factorySpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Returns the algorithm name of this <code>TrustManagerFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <code>TrustManagerFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return the algorithm name of this <code>TrustManagerFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Generates a <code>TrustManagerFactory</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * specified trust management algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * If the default provider package provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * requested trust management algorithm, an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <code>TrustManagerFactory</code> containing that implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * returned.  If the algorithm is not available in the default provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * package, other provider packages are searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param algorithm the standard name of the requested trust management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return the new <code>TrustManagerFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @exception NoSuchAlgorithmException if the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * available in the default provider package or any of the other provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * packages that were searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static final TrustManagerFactory getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            Object[] objs = SSLSecurity.getImpl(algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                "TrustManagerFactory", (String) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                    (Provider)objs[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                    algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new NoSuchAlgorithmException(algorithm + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Generates a <code>TrustManagerFactory</code> object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * trust management algorithm from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param algorithm the standard name of the requested trust management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param provider the name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the new <code>TrustManagerFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception NoSuchAlgorithmException if the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception NoSuchProviderException if the specified provider has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * been configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public static final TrustManagerFactory getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                                 String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (provider == null || provider.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Object[] objs = SSLSecurity.getImpl(algorithm, "TrustManagerFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                            provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            (Provider)objs[1], algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Generates a <code>TrustManagerFactory</code> object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * trust management algorithm from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param algorithm the standard name of the requested trust management
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param provider an instance of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return the new <code>TrustManagerFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @exception NoSuchAlgorithmException if the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public static final TrustManagerFactory getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                                 Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (provider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Object[] objs = SSLSecurity.getImpl(algorithm, "TrustManagerFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                            provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            (Provider)objs[1], algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns the provider of this <code>TrustManagerFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return the provider of this <code>TrustManagerFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Initializes this factory with a source of certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * authorities and related trust material. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * provider may also include a provider-specific source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * of key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param ks the key store or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void init(KeyStore ks) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        factorySpi.engineInit(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Returns one trust manager for each type of trust material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return the trust managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public TrustManager[] getTrustManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return factorySpi.engineGetTrustManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}