src/java.base/share/classes/sun/security/provider/certpath/CollectionCertStore.java
author weijun
Wed, 01 Aug 2018 13:35:08 +0800
changeset 51272 9d92ff04a29c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208602: Cannot read PEM X.509 cert if there is whitespace after the header or footer Reviewed-by: xuelei
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: 5506
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ConcurrentModificationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.CertSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CertStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.CertStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.CertStoreParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.CollectionCertStoreParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.CRLSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.CertStoreSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A <code>CertStore</code> that retrieves <code>Certificates</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>CRL</code>s from a <code>Collection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Before calling the {@link #engineGetCertificates engineGetCertificates} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * {@link #engineGetCRLs engineGetCRLs} methods, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * {@link #CollectionCertStore(CertStoreParameters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * CollectionCertStore(CertStoreParameters)} constructor is called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * create the <code>CertStore</code> and establish the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>Collection</code> from which <code>Certificate</code>s and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>CRL</code>s will be retrieved. If the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>Collection</code> contains an object that is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <code>Certificate</code> or <code>CRL</code>, that object will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * As described in the javadoc for <code>CertStoreSpi</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <code>engineGetCertificates</code> and <code>engineGetCRLs</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * must be thread-safe. That is, multiple threads may concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * invoke these methods on a single <code>CollectionCertStore</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * object (or more than one) with no ill effects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * This is achieved by requiring that the <code>Collection</code> passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * the {@link #CollectionCertStore(CertStoreParameters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * CollectionCertStore(CertStoreParameters)} constructor (via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <code>CollectionCertStoreParameters</code> object) must have fail-fast
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * iterators. Simultaneous modifications to the <code>Collection</code> can thus be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * detected and certificate or CRL retrieval can be retried. The fact that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <code>Certificate</code>s and <code>CRL</code>s must be thread-safe is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * essential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see java.security.cert.CertStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @author      Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public class CollectionCertStore extends CertStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private Collection<?> coll;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Creates a <code>CertStore</code> with the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * For this class, the parameters object must be an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <code>CollectionCertStoreParameters</code>. The <code>Collection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * included in the <code>CollectionCertStoreParameters</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * must be thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception InvalidAlgorithmParameterException if params is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *   instance of <code>CollectionCertStoreParameters</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public CollectionCertStore(CertStoreParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        super(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (!(params instanceof CollectionCertStoreParameters))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new InvalidAlgorithmParameterException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                "parameters must be CollectionCertStoreParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        coll = ((CollectionCertStoreParameters) params).getCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns a <code>Collection</code> of <code>Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * match the specified selector. If no <code>Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param selector a <code>CertSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *  <code>Certificate</code>s should be returned. Specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *  to return all <code>Certificate</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return a <code>Collection</code> of <code>Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *         match the specified selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   116
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public Collection<Certificate> engineGetCertificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            (CertSelector selector) throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (coll == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new CertStoreException("Collection is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // Tolerate a few ConcurrentModificationExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (int c = 0; c < 10; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   125
                HashSet<Certificate> result = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (selector != null) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   127
                    for (Object o : coll) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        if ((o instanceof Certificate) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                            selector.match((Certificate) o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                            result.add((Certificate)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   133
                    for (Object o : coll) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        if (o instanceof Certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            result.add((Certificate)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } catch (ConcurrentModificationException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        throw new ConcurrentModificationException("Too many "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            + "ConcurrentModificationExceptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Returns a <code>Collection</code> of <code>CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * match the specified selector. If no <code>CRL</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param selector a <code>CRLSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *  <code>CRL</code>s should be returned. Specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *  to return all <code>CRL</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return a <code>Collection</code> of <code>CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         match the specified selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   157
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public Collection<CRL> engineGetCRLs(CRLSelector selector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        throws CertStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (coll == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new CertStoreException("Collection is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // Tolerate a few ConcurrentModificationExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        for (int c = 0; c < 10; c++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            try {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   167
                HashSet<CRL> result = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (selector != null) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   169
                    for (Object o : coll) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        if ((o instanceof CRL) && selector.match((CRL) o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            result.add((CRL)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   174
                    for (Object o : coll) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        if (o instanceof CRL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                            result.add((CRL)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   179
                return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } catch (ConcurrentModificationException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        throw new ConcurrentModificationException("Too many "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            + "ConcurrentModificationExceptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}