jdk/src/share/classes/sun/security/provider/certpath/URICertStore.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 10336 0bb1999251f8
child 10782 01689c7b34ac
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9734
diff changeset
     2
 * Copyright (c) 2006, 2011, 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: 4039
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: 4039
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: 4039
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4039
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4039
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.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URLConnection;
4039
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
    33
import java.security.AccessController;
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
    34
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.CertSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.CertStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.CertStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.CertStoreParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.CertStoreSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.CRLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.cert.CRLSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.cert.X509CertSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.cert.X509CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.cert.X509CRLSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.List;
6122
16fa7ed7ff1b 6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents: 5506
diff changeset
    55
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.security.x509.AccessDescription;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.security.x509.GeneralNameInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.security.x509.URIName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.security.util.Cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * A <code>CertStore</code> that retrieves <code>Certificates</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>CRL</code>s from a URI, for example, as specified in an X.509
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * AuthorityInformationAccess or CRLDistributionPoint extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * For CRLs, this implementation retrieves a single DER encoded CRL per URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * For Certificates, this implementation retrieves a single DER encoded CRL or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * a collection of Certificates encoded as a PKCS#7 "certs-only" CMS message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * This <code>CertStore</code> also implements Certificate/CRL caching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Currently, the cache is shared between all applications in the VM and uses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * hardcoded policy. The cache has a maximum size of 185 entries, which are held
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * by SoftReferences. A request will be satisfied from the cache if we last
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * checked for an update within CHECK_INTERVAL (last 30 seconds). Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * we open an URLConnection to download the Certificate(s)/CRL using an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * If-Modified-Since request (HTTP) if possible. Note that both positive and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * negative responses are cached, i.e. if we are unable to open the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * or the Certificate(s)/CRL cannot be parsed, we remember this result and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * additional calls during the CHECK_INTERVAL period do not try to open another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * The URICertStore is not currently a standard CertStore type. We should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * consider adding a standard "URI" CertStore type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @since 7.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
class URICertStore extends CertStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // interval between checks for update of cached Certificates/CRLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // (30 seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private final static int CHECK_INTERVAL = 30 * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // size of the cache (see Cache class for sizing recommendations)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private final static int CACHE_SIZE = 185;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // X.509 certificate factory instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final CertificateFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // cached Collection of X509Certificates (may be empty, never null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private Collection<X509Certificate> certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Collections.<X509Certificate>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // cached X509CRL (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private X509CRL crl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // time we last checked for an update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private long lastChecked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // time server returned as last modified time stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // or 0 if not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private long lastModified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // the URI of this CertStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private URI uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // true if URI is ldap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private boolean ldap = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private CertStore ldapCertStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private String ldapPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
4039
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   127
     * Holder class to lazily load LDAPCertStoreHelper if present.
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   128
     */
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   129
    private static class LDAP {
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   130
        private static final String CERT_STORE_HELPER =
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   131
            "sun.security.provider.certpath.ldap.LDAPCertStoreHelper";
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   132
        private static final CertStoreHelper helper =
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   133
            AccessController.doPrivileged(
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   134
                new PrivilegedAction<CertStoreHelper>() {
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   135
                    public CertStoreHelper run() {
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   136
                        try {
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   137
                            Class<?> c = Class.forName(CERT_STORE_HELPER, true, null);
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   138
                            return (CertStoreHelper)c.newInstance();
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   139
                        } catch (ClassNotFoundException cnf) {
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   140
                            return null;
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   141
                        } catch (InstantiationException e) {
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   142
                            throw new AssertionError(e);
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   143
                        } catch (IllegalAccessException e) {
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   144
                            throw new AssertionError(e);
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   145
                        }
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   146
                    }});
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   147
        static CertStoreHelper helper() {
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   148
            return helper;
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   149
        }
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   150
    }
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   151
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   152
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Creates a URICertStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param parameters specifying the URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    URICertStore(CertStoreParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        super(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (!(params instanceof URICertStoreParameters)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                ("params must be instanceof URICertStoreParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.uri = ((URICertStoreParameters) params).uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // if ldap URI, use an LDAPCertStore to fetch certs and CRLs
6122
16fa7ed7ff1b 6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents: 5506
diff changeset
   166
        if (uri.getScheme().toLowerCase(Locale.ENGLISH).equals("ldap")) {
4039
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   167
            if (LDAP.helper() == null)
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   168
                throw new NoSuchAlgorithmException("LDAP not present");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            ldap = true;
4039
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   170
            ldapCertStore = LDAP.helper().getCertStore(uri);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            ldapPath = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // strip off leading '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (ldapPath.charAt(0) == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                ldapPath = ldapPath.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            factory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns a URI CertStore. This method consults a cache of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * CertStores (shared per JVM) using the URI as a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private static final Cache certStoreCache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Cache.newSoftMemoryCache(CACHE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static synchronized CertStore getInstance(URICertStoreParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            debug.println("CertStore URI:" + params.uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        CertStore ucs = (CertStore) certStoreCache.get(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (ucs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            ucs = new UCS(new URICertStore(params), null, "URI", params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            certStoreCache.put(params, ucs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                debug.println("URICertStore.getInstance: cache hit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return ucs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Creates a CertStore from information included in the AccessDescription
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * object of a certificate's Authority Information Access Extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static CertStore getInstance(AccessDescription ad) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9734
diff changeset
   212
        if (!ad.getAccessMethod().equals((Object)
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9734
diff changeset
   213
                AccessDescription.Ad_CAISSUERS_Id)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        GeneralNameInterface gn = ad.getAccessLocation().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (!(gn instanceof URIName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        URI uri = ((URIName) gn).getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return URICertStore.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                (new URICertStore.URICertStoreParameters(uri));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                debug.println("exception creating CertStore: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * match the specified selector. If no <code>X509Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param selector a <code>CertSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *  <code>X509Certificate</code>s should be returned. Specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *  <code>null</code> to return all <code>X509Certificate</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return a <code>Collection</code> of <code>X509Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *         match the specified selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9734
diff changeset
   245
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public synchronized Collection<X509Certificate> engineGetCertificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        (CertSelector selector) throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // if ldap URI we wrap the CertSelector in an LDAPCertSelector to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // avoid LDAP DN matching issues (see LDAPCertSelector for more info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (ldap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            X509CertSelector xsel = (X509CertSelector) selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            try {
4039
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   254
                xsel = LDAP.helper().wrap(xsel, xsel.getSubject(), ldapPath);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                throw new CertStoreException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // Fetch the certificates via LDAP. LDAPCertStore has its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // caching mechanism, see the class description for more info.
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9734
diff changeset
   260
            // Safe cast since xsel is an X509 certificate selector.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return (Collection<X509Certificate>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                ldapCertStore.getCertificates(xsel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // Return the Certificates for this entry. It returns the cached value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // if it is still current and fetches the Certificates otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // For the caching details, see the top of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (time - lastChecked < CHECK_INTERVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                debug.println("Returning certificates from cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return getMatchingCerts(certs, selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        lastChecked = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            URLConnection connection = uri.toURL().openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (lastModified != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                connection.setIfModifiedSince(lastModified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            in = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            long oldLastModified = lastModified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            lastModified = connection.getLastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (oldLastModified != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (oldLastModified == lastModified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        debug.println("Not modified, using cached copy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    return getMatchingCerts(certs, selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                } else if (connection instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    // some proxy servers omit last modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    HttpURLConnection hconn = (HttpURLConnection) connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    if (hconn.getResponseCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            debug.println("Not modified, using cached copy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        return getMatchingCerts(certs, selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                debug.println("Downloading new certificates...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9734
diff changeset
   306
            // Safe cast since factory is an X.509 certificate factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            certs = (Collection<X509Certificate>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                factory.generateCertificates(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return getMatchingCerts(certs, selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                debug.println("Exception fetching certificates:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                debug.println("Exception fetching certificates:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // exception, forget previous values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        lastModified = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        certs = Collections.<X509Certificate>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Iterates over the specified Collection of X509Certificates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * returns only those that match the criteria specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * CertSelector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private static Collection<X509Certificate> getMatchingCerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        (Collection<X509Certificate> certs, CertSelector selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // if selector not specified, all certs match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (selector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        List<X509Certificate> matchedCerts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            new ArrayList<X509Certificate>(certs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        for (X509Certificate cert : certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (selector.match(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                matchedCerts.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return matchedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Returns a <code>Collection</code> of <code>X509CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * match the specified selector. If no <code>X509CRL</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param selector A <code>CRLSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *  <code>X509CRL</code>s should be returned. Specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *  to return all <code>X509CRL</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return A <code>Collection</code> of <code>X509CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         match the specified selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9734
diff changeset
   368
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public synchronized Collection<X509CRL> engineGetCRLs(CRLSelector selector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // if ldap URI we wrap the CRLSelector in an LDAPCRLSelector to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // avoid LDAP DN matching issues (see LDAPCRLSelector for more info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (ldap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            X509CRLSelector xsel = (X509CRLSelector) selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            try {
4039
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
   377
                xsel = LDAP.helper().wrap(xsel, null, ldapPath);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                throw new CertStoreException(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            // Fetch the CRLs via LDAP. LDAPCertStore has its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            // caching mechanism, see the class description for more info.
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9734
diff changeset
   383
            // Safe cast since xsel is an X509 certificate selector.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return (Collection<X509CRL>) ldapCertStore.getCRLs(xsel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Return the CRLs for this entry. It returns the cached value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // if it is still current and fetches the CRLs otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // For the caching details, see the top of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (time - lastChecked < CHECK_INTERVAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                debug.println("Returning CRL from cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return getMatchingCRLs(crl, selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        lastChecked = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            URLConnection connection = uri.toURL().openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (lastModified != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                connection.setIfModifiedSince(lastModified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            in = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            long oldLastModified = lastModified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            lastModified = connection.getLastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (oldLastModified != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                if (oldLastModified == lastModified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        debug.println("Not modified, using cached copy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    return getMatchingCRLs(crl, selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                } else if (connection instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    // some proxy servers omit last modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    HttpURLConnection hconn = (HttpURLConnection) connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (hconn.getResponseCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                == HttpURLConnection.HTTP_NOT_MODIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                            debug.println("Not modified, using cached copy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        return getMatchingCRLs(crl, selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                debug.println("Downloading new CRL...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            crl = (X509CRL) factory.generateCRL(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return getMatchingCRLs(crl, selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                debug.println("Exception fetching CRL:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        } catch (CRLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                debug.println("Exception fetching CRL:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // exception, forget previous values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        lastModified = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        crl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return Collections.<X509CRL>emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Checks if the specified X509CRL matches the criteria specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * CRLSelector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    private static Collection<X509CRL> getMatchingCRLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        (X509CRL crl, CRLSelector selector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (selector == null || (crl != null && selector.match(crl))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return Collections.<X509CRL>singletonList(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return Collections.<X509CRL>emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * CertStoreParameters for the URICertStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    static class URICertStoreParameters implements CertStoreParameters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        private final URI uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        private volatile int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        URICertStoreParameters(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            this.uri = uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (!(obj instanceof URICertStoreParameters)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            URICertStoreParameters params = (URICertStoreParameters) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            return uri.equals(params.uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (hashCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                result = 37*result + uri.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                hashCode = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                /* Cannot happen */
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10336
diff changeset
   497
                throw new InternalError(e.toString(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * This class allows the URICertStore to be accessed as a CertStore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    private static class UCS extends CertStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        protected UCS(CertStoreSpi spi, Provider p, String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            CertStoreParameters params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            super(spi, p, type, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
}