jdk/src/share/classes/java/security/cert/CertStore.java
author xuelei
Wed, 20 Jan 2010 21:38:37 +0800
changeset 4807 2521b7dcf505
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6862064: incorrect implementation of PKIXParameters.clone() Reviewed-by: weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 java.security.cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A class for retrieving <code>Certificate</code>s and <code>CRL</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * from a repository.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class uses a provider-based architecture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * To create a <code>CertStore</code>, call one of the static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>getInstance</code> methods, passing in the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <code>CertStore</code> desired, any applicable initialization parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * and optionally the name of the provider desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Once the <code>CertStore</code> has been created, it can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * retrieve <code>Certificate</code>s and <code>CRL</code>s by calling its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@link #getCertificates(CertSelector selector) getCertificates} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link #getCRLs(CRLSelector selector) getCRLs} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Unlike a {@link java.security.KeyStore KeyStore}, which provides access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * to a cache of private keys and trusted certificates, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>CertStore</code> is designed to provide access to a potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * vast repository of untrusted certificates and CRLs. For example, an LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * implementation of <code>CertStore</code> provides access to certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * and CRLs stored in one or more directories using the LDAP protocol and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * schema as defined in the RFC service attribute. See Appendix A in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <a href= "../../../../technotes/guides/security/certpath/CertPathProgGuide.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Java Certification Path API Programmer's Guide</a> for more information about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * standard <code>CertStore</code> types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * All public methods of <code>CertStore</code> objects must be thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * That is, multiple threads may concurrently invoke these methods on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * single <code>CertStore</code> object (or more than one) with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * ill effects. This allows a <code>CertPathBuilder</code> to search for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * CRL while simultaneously searching for further certificates, for instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The static methods of this class are also guaranteed to be thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Multiple threads may concurrently invoke the static methods defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * this class with no ill effects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * @author      Sean Mullan, Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class CertStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Constant to lookup in the Security properties file to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * the default certstore type. In the Security properties file, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * default certstore type is given as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * certstore.type=LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final String CERTSTORE_TYPE = "certstore.type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private CertStoreSpi storeSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private CertStoreParameters params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Creates a <code>CertStore</code> object of the given type, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * encapsulates the given provider implementation (SPI object) in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param storeSpi the provider implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param type the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param params the initialization parameters (may be <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected CertStore(CertStoreSpi storeSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        String type, CertStoreParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.storeSpi = storeSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (params != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            this.params = (CertStoreParameters) params.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns a <code>Collection</code> of <code>Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * match the specified selector. If no <code>Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * For some <code>CertStore</code> types, the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <code>Collection</code> may not contain <b>all</b> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <code>Certificate</code>s that match the selector. For instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * an LDAP <code>CertStore</code> may not search all entries in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * directory. Instead, it may just search entries that are likely to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * contain the <code>Certificate</code>s it is looking for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Some <code>CertStore</code> implementations (especially LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <code>CertStore</code>s) may throw a <code>CertStoreException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * unless a non-null <code>CertSelector</code> is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * includes specific criteria that can be used to find the certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Issuer and/or subject names are especially useful criteria.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param selector A <code>CertSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *  <code>Certificate</code>s should be returned. Specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *  to return all <code>Certificate</code>s (if supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return A <code>Collection</code> of <code>Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *         match the specified selector (never <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public final Collection<? extends Certificate> getCertificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            (CertSelector selector) throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return storeSpi.engineGetCertificates(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns a <code>Collection</code> of <code>CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * match the specified selector. If no <code>CRL</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * For some <code>CertStore</code> types, the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <code>Collection</code> may not contain <b>all</b> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <code>CRL</code>s that match the selector. For instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * an LDAP <code>CertStore</code> may not search all entries in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * directory. Instead, it may just search entries that are likely to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * contain the <code>CRL</code>s it is looking for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Some <code>CertStore</code> implementations (especially LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>CertStore</code>s) may throw a <code>CertStoreException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * unless a non-null <code>CRLSelector</code> is provided that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * includes specific criteria that can be used to find the CRLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Issuer names and/or the certificate to be checked are especially useful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param selector A <code>CRLSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *  <code>CRL</code>s should be returned. Specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *  to return all <code>CRL</code>s (if supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return A <code>Collection</code> of <code>CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *         match the specified selector (never <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public final Collection<? extends CRL> getCRLs(CRLSelector selector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return storeSpi.engineGetCRLs(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns a <code>CertStore</code> object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>CertStore</code> type and is initialized with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * A new CertStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * CertStoreSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Provider that supports the specified type is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p>The <code>CertStore</code> that is returned is initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * specified <code>CertStoreParameters</code>. The type of parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * needed may vary between different types of <code>CertStore</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Note that the specified <code>CertStoreParameters</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param type the name of the requested <code>CertStore</code> type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *  See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *  "../../../../technotes/guides/security/certpath/CertPathProgGuide.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *  Java Certification Path API Programmer's Guide </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *  for information about standard types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param params the initialization parameters (may be <code>null</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return a <code>CertStore</code> object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          <code>CertStore</code> type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @throws NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *          CertStoreSpi implementation for the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @throws InvalidAlgorithmParameterException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          initialization parameters are inappropriate for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *          <code>CertStore</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public static CertStore getInstance(String type, CertStoreParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throws InvalidAlgorithmParameterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            Instance instance = GetInstance.getInstance("CertStore",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                CertStoreSpi.class, type, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return new CertStore((CertStoreSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                instance.provider, type, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return handleException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private static CertStore handleException(NoSuchAlgorithmException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Throwable cause = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (cause instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw (InvalidAlgorithmParameterException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns a <code>CertStore</code> object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <code>CertStore</code> type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <p> A new CertStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * CertStoreSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p>The <code>CertStore</code> that is returned is initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * specified <code>CertStoreParameters</code>. The type of parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * needed may vary between different types of <code>CertStore</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Note that the specified <code>CertStoreParameters</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param type the requested <code>CertStore</code> type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *  See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *  "../../../../technotes/guides/security/certpath/CertPathProgGuide.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *  Java Certification Path API Programmer's Guide </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *  for information about standard types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param params the initialization parameters (may be <code>null</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return a <code>CertStore</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *          specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @throws NoSuchAlgorithmException if a CertStoreSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          implementation for the specified type is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws InvalidAlgorithmParameterException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *          initialization parameters are inappropriate for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          <code>CertStore</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @throws NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception IllegalArgumentException if the <code>provider</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *          null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public static CertStore getInstance(String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            CertStoreParameters params, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throws InvalidAlgorithmParameterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            Instance instance = GetInstance.getInstance("CertStore",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                CertStoreSpi.class, type, params, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return new CertStore((CertStoreSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                instance.provider, type, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return handleException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Returns a <code>CertStore</code> object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <code>CertStore</code> type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p> A new CertStore object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * CertStoreSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <p>The <code>CertStore</code> that is returned is initialized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * specified <code>CertStoreParameters</code>. The type of parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * needed may vary between different types of <code>CertStore</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Note that the specified <code>CertStoreParameters</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param type the requested <code>CertStore</code> type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *  See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *  "../../../../technotes/guides/security/certpath/CertPathProgGuide.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *  Java Certification Path API Programmer's Guide </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *  for information about standard types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param params the initialization parameters (may be <code>null</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return a <code>CertStore</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *          specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @exception NoSuchAlgorithmException if a CertStoreSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *          implementation for the specified type is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @throws InvalidAlgorithmParameterException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *          initialization parameters are inappropriate for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *          <code>CertStore</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @exception IllegalArgumentException if the <code>provider</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public static CertStore getInstance(String type, CertStoreParameters params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            Provider provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            Instance instance = GetInstance.getInstance("CertStore",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                CertStoreSpi.class, type, params, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return new CertStore((CertStoreSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                instance.provider, type, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return handleException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Returns the parameters used to initialize this <code>CertStore</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Note that the <code>CertStoreParameters</code> object is cloned before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * it is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return the parameters used to initialize this <code>CertStore</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * (may be <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public final CertStoreParameters getCertStoreParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return (params == null ? null : (CertStoreParameters) params.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Returns the type of this <code>CertStore</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @return the type of this <code>CertStore</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public final String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return this.type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Returns the provider of this <code>CertStore</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @return the provider of this <code>CertStore</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Returns the default <code>CertStore</code> type as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Java security properties file, or the string &quot;LDAP&quot; if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * such property exists. The Java security properties file is located in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * the file named &lt;JAVA_HOME&gt;/lib/security/java.security.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * &lt;JAVA_HOME&gt; refers to the value of the java.home system property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * and specifies the directory where the JRE is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <p>The default <code>CertStore</code> type can be used by applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * that do not want to use a hard-coded type when calling one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <code>getInstance</code> methods, and want to provide a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <code>CertStore</code> type in case a user does not specify its own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p>The default <code>CertStore</code> type can be changed by setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * the value of the "certstore.type" security property (in the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * security properties file) to the desired type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @return the default <code>CertStore</code> type as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Java security properties file, or the string &quot;LDAP&quot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * if no such property exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public final static String getDefaultType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        String cstype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        cstype = AccessController.doPrivileged(new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                return Security.getProperty(CERTSTORE_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (cstype == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            cstype = "LDAP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return cstype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}