src/java.base/share/classes/sun/security/provider/certpath/IndexedCollectionCertStore.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/classes/sun/security/provider/certpath/IndexedCollectionCertStore.java@3317bb8137f4
child 51986 c1db377f6300
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A <code>CertStore</code> that retrieves <code>Certificates</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>CRL</code>s from a <code>Collection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This implementation is functionally equivalent to CollectionCertStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * with two differences:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <li>Upon construction, the elements in the specified Collection are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * partially indexed. X509Certificates are indexed by subject, X509CRLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * by issuer, non-X509 Certificates and CRLs are copied without indexing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * other objects are ignored. This increases CertStore construction time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * but allows significant speedups for searches which specify the indexed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * attributes, in particular for large Collections (reduction from linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * time to effectively constant time). Searches for non-indexed queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * are as fast (or marginally faster) than for the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * CollectionCertStore. Certificate subjects and CRL issuers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * were found to be specified in most searches used internally by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * CertPath provider. Additional attributes could indexed if there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * queries that justify the effort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <li>Changes to the specified Collection after construction time are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * not detected and ignored. This is because there is no way to efficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * detect if a Collection has been modified, a full traversal would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * required. That would degrade lookup performance to linear time and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * eliminated the benefit of indexing. We may fix this via the introduction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * of new public APIs in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Before calling the {@link #engineGetCertificates engineGetCertificates} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * {@link #engineGetCRLs engineGetCRLs} methods, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * {@link #CollectionCertStore(CertStoreParameters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * CollectionCertStore(CertStoreParameters)} constructor is called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * create the <code>CertStore</code> and establish the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <code>Collection</code> from which <code>Certificate</code>s and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <code>CRL</code>s will be retrieved. If the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <code>Collection</code> contains an object that is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <code>Certificate</code> or <code>CRL</code>, that object will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * As described in the javadoc for <code>CertStoreSpi</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <code>engineGetCertificates</code> and <code>engineGetCRLs</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * must be thread-safe. That is, multiple threads may concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * invoke these methods on a single <code>CollectionCertStore</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * object (or more than one) with no ill effects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * This is achieved by requiring that the <code>Collection</code> passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the {@link #CollectionCertStore(CertStoreParameters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * CollectionCertStore(CertStoreParameters)} constructor (via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code>CollectionCertStoreParameters</code> object) must have fail-fast
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * iterators. Simultaneous modifications to the <code>Collection</code> can thus be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * detected and certificate or CRL retrieval can be retried. The fact that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <code>Certificate</code>s and <code>CRL</code>s must be thread-safe is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * essential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @see java.security.cert.CertStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @see CollectionCertStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
public class IndexedCollectionCertStore extends CertStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Map X500Principal(subject) -> X509Certificate | List of X509Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private Map<X500Principal, Object> certSubjects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Map X500Principal(issuer) -> X509CRL | List of X509CRL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private Map<X500Principal, Object> crlIssuers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Sets of non-X509 certificates and CRLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private Set<Certificate> otherCertificates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private Set<CRL> otherCRLs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Creates a <code>CertStore</code> with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * For this class, the parameters object must be an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <code>CollectionCertStoreParameters</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @exception InvalidAlgorithmParameterException if params is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *   instance of <code>CollectionCertStoreParameters</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public IndexedCollectionCertStore(CertStoreParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        super(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (!(params instanceof CollectionCertStoreParameters)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new InvalidAlgorithmParameterException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                "parameters must be CollectionCertStoreParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Collection<?> coll = ((CollectionCertStoreParameters)params).getCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (coll == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                        ("Collection must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        buildIndex(coll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Index the specified Collection copying all references to Certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * and CRLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private void buildIndex(Collection<?> coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        certSubjects = new HashMap<X500Principal, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        crlIssuers = new HashMap<X500Principal, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        otherCertificates = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        otherCRLs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (Object obj : coll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (obj instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                indexCertificate((X509Certificate)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } else if (obj instanceof X509CRL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                indexCRL((X509CRL)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else if (obj instanceof Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (otherCertificates == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    otherCertificates = new HashSet<Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                otherCertificates.add((Certificate)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } else if (obj instanceof CRL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (otherCRLs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    otherCRLs = new HashSet<CRL>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                otherCRLs.add((CRL)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (otherCertificates == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            otherCertificates = Collections.<Certificate>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (otherCRLs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            otherCRLs = Collections.<CRL>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Add an X509Certificate to the index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private void indexCertificate(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        X500Principal subject = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        Object oldEntry = certSubjects.put(subject, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (oldEntry != null) { // assume this is unlikely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (oldEntry instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (cert.equals(oldEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   183
                List<X509Certificate> list = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                list.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                list.add((X509Certificate)oldEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                certSubjects.put(subject, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   188
                @SuppressWarnings("unchecked") // See certSubjects javadoc.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                List<X509Certificate> list = (List<X509Certificate>)oldEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (list.contains(cert) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    list.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                certSubjects.put(subject, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Add an X509CRL to the index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private void indexCRL(X509CRL crl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        X500Principal issuer = crl.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Object oldEntry = crlIssuers.put(issuer, crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (oldEntry != null) { // assume this is unlikely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (oldEntry instanceof X509CRL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (crl.equals(oldEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   209
                List<X509CRL> list = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                list.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                list.add((X509CRL)oldEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                crlIssuers.put(issuer, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   214
                // See crlIssuers javadoc.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   215
                @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                List<X509CRL> list = (List<X509CRL>)oldEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                if (list.contains(crl) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    list.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                crlIssuers.put(issuer, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns a <code>Collection</code> of <code>Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * match the specified selector. If no <code>Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param selector a <code>CertSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *  <code>Certificate</code>s should be returned. Specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *  to return all <code>Certificate</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return a <code>Collection</code> of <code>Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *         match the specified selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   237
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public Collection<? extends Certificate> engineGetCertificates(CertSelector selector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // no selector means match all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (selector == null) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   243
            Set<Certificate> matches = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            matchX509Certs(new X509CertSelector(), matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            matches.addAll(otherCertificates);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (selector instanceof X509CertSelector == false) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   250
            Set<Certificate> matches = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            matchX509Certs(selector, matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            for (Certificate cert : otherCertificates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if (selector.match(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    matches.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (certSubjects.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return Collections.<X509Certificate>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        X509CertSelector x509Selector = (X509CertSelector)selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // see if the subject is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        X500Principal subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        X509Certificate matchCert = x509Selector.getCertificate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (matchCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            subject = matchCert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            subject = x509Selector.getSubject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (subject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // yes, narrow down candidates to indexed possibilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            Object entry = certSubjects.get(subject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return Collections.<X509Certificate>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (entry instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                X509Certificate x509Entry = (X509Certificate)entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if (x509Selector.match(x509Entry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    return Collections.singleton(x509Entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    return Collections.<X509Certificate>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   286
                // See certSubjects javadoc.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   287
                @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                List<X509Certificate> list = (List<X509Certificate>)entry;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   289
                Set<X509Certificate> matches = new HashSet<>(16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                for (X509Certificate cert : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    if (x509Selector.match(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        matches.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // cannot use index, iterate all
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   299
        Set<Certificate> matches = new HashSet<>(16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        matchX509Certs(x509Selector, matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Iterate through all the X509Certificates and add matches to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private void matchX509Certs(CertSelector selector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Collection<Certificate> matches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        for (Object obj : certSubjects.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (obj instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                X509Certificate cert = (X509Certificate)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (selector.match(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    matches.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   318
                // See certSubjects javadoc.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   319
                @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                List<X509Certificate> list = (List<X509Certificate>)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                for (X509Certificate cert : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    if (selector.match(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        matches.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Returns a <code>Collection</code> of <code>CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * match the specified selector. If no <code>CRL</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param selector a <code>CRLSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *  <code>CRL</code>s should be returned. Specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *  to return all <code>CRL</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return a <code>Collection</code> of <code>CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *         match the specified selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   342
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public Collection<CRL> engineGetCRLs(CRLSelector selector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (selector == null) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   347
            Set<CRL> matches = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            matchX509CRLs(new X509CRLSelector(), matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            matches.addAll(otherCRLs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (selector instanceof X509CRLSelector == false) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   354
            Set<CRL> matches = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            matchX509CRLs(selector, matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            for (CRL crl : otherCRLs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                if (selector.match(crl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    matches.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (crlIssuers.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return Collections.<CRL>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        X509CRLSelector x509Selector = (X509CRLSelector)selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // see if the issuer is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        Collection<X500Principal> issuers = x509Selector.getIssuers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (issuers != null) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   371
            HashSet<CRL> matches = new HashSet<>(16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            for (X500Principal issuer : issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                Object entry = crlIssuers.get(issuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                } else if (entry instanceof X509CRL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    X509CRL crl = (X509CRL)entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    if (x509Selector.match(crl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        matches.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                } else { // List
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   382
                    // See crlIssuers javadoc.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   383
                    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    List<X509CRL> list = (List<X509CRL>)entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    for (X509CRL crl : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        if (x509Selector.match(crl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                            matches.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        // cannot use index, iterate all
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   395
        Set<CRL> matches = new HashSet<>(16);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        matchX509CRLs(x509Selector, matches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Iterate through all the X509CRLs and add matches to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    private void matchX509CRLs(CRLSelector selector, Collection<CRL> matches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        for (Object obj : crlIssuers.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (obj instanceof X509CRL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                X509CRL crl = (X509CRL)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                if (selector.match(crl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    matches.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            } else {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   412
                // See crlIssuers javadoc.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   413
                @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                List<X509CRL> list = (List<X509CRL>)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                for (X509CRL crl : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (selector.match(crl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        matches.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
}