src/java.naming/share/classes/sun/security/provider/certpath/ldap/LDAPCertStore.java
author psadhukhan
Wed, 25 Sep 2019 14:48:39 +0530
changeset 58330 de8e08015d51
parent 48575 2ce508de5c77
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48575
2ce508de5c77 8178458: Better use of certificates in LDAP
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2017, 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
4039
afadb206ca44 6889552: Sun provider should not require LDAP CertStore to be present
alanb
parents: 2
diff changeset
    26
package sun.security.provider.certpath.ldap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.*;
32502
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
    31
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.Cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.Debug;
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 an LDAP directory, using the PKIX LDAP V2 Schema
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * (RFC 2587):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <a href="http://www.ietf.org/rfc/rfc2587.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * http://www.ietf.org/rfc/rfc2587.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Before calling the {@link #engineGetCertificates engineGetCertificates} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * {@link #engineGetCRLs engineGetCRLs} methods, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * {@link #LDAPCertStore(CertStoreParameters)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * LDAPCertStore(CertStoreParameters)} constructor is called to create the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>CertStore</code> and establish the DNS name and port of the LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * server from which <code>Certificate</code>s and <code>CRL</code>s will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * retrieved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * As described in the javadoc for <code>CertStoreSpi</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>engineGetCertificates</code> and <code>engineGetCRLs</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * must be thread-safe. That is, multiple threads may concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * invoke these methods on a single <code>LDAPCertStore</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * (or more than one) with no ill effects. This allows a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>CertPathBuilder</code> to search for a CRL while simultaneously
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * searching for further certificates, for instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * This is achieved by adding the <code>synchronized</code> keyword to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <code>engineGetCertificates</code> and <code>engineGetCRLs</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * This classes uses caching and requests multiple attributes at once to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * minimize LDAP round trips. The cache is associated with the CertStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * instance. It uses soft references to hold the values to minimize impact
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * on footprint and currently has a maximum size of 750 attributes and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * 30 second default lifetime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * We always request CA certificates, cross certificate pairs, and ARLs in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * a single LDAP request when any one of them is needed. The reason is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * we typically need all of them anyway and requesting them in one go can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * reduce the number of requests to a third. Even if we don't need them,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * these attributes are typically small enough not to cause a noticeable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * overhead. In addition, when the prefetchCRLs flag is true, we also request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * the full CRLs. It is currently false initially but set to true once any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * request for an ARL to the server returns an null value. The reason is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * that CRLs could be rather large but are rarely used. This implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * should improve performance in most cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @see java.security.cert.CertStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author      Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author      Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
10782
01689c7b34ac 6953295: Move few sun.security.{util, x509, pkcs} classes used by keytool/jarsigner to another package
mullan
parents: 10369
diff changeset
    86
public final class LDAPCertStore extends CertStoreSpi {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    90
    private String ldapDN;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    91
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    92
    private LDAPCertStoreImpl impl;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    93
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    94
    public LDAPCertStore(CertStoreParameters params)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    95
        throws InvalidAlgorithmParameterException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    96
        super(params);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    98
        String serverName;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
    99
        int port;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   100
        String dn = null;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   101
        if (params == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   102
            throw new InvalidAlgorithmParameterException(
32502
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   103
                    "Parameters required for LDAP certstore");
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   104
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   105
        if (params instanceof LDAPCertStoreParameters) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   106
            LDAPCertStoreParameters p = (LDAPCertStoreParameters) params;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   107
            serverName = p.getServerName();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   108
            port = p.getPort();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   109
        } else if (params instanceof URICertStoreParameters) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   110
            URICertStoreParameters p = (URICertStoreParameters) params;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   111
            URI u = p.getURI();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   112
            if (!u.getScheme().equalsIgnoreCase("ldap")) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   113
                throw new InvalidAlgorithmParameterException(
32502
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   114
                        "Unsupported scheme '" + u.getScheme()
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   115
                                + "', only LDAP URIs are supported "
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   116
                                + "for LDAP certstore");
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   117
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   118
            // Use the same default values as in LDAPCertStoreParameters
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   119
            // if unspecified in URI
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   120
            serverName = u.getHost();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   121
            if (serverName == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   122
                serverName = "localhost";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   123
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   124
            port = u.getPort();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   125
            if (port == -1) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   126
                port = 389;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   127
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   128
            dn = u.getPath();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   129
            if (dn != null && dn.charAt(0) == '/') {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   130
                dn = dn.substring(1);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   131
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   132
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   133
            throw new InvalidAlgorithmParameterException(
32502
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   134
                "Parameters must be either LDAPCertStoreParameters or "
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   135
                        + "URICertStoreParameters, but instance of "
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   136
                        + params.getClass().getName() + " passed");
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   137
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
48575
2ce508de5c77 8178458: Better use of certificates in LDAP
weijun
parents: 47216
diff changeset
   139
        SecurityManager security = System.getSecurityManager();
2ce508de5c77 8178458: Better use of certificates in LDAP
weijun
parents: 47216
diff changeset
   140
        if (security != null) {
2ce508de5c77 8178458: Better use of certificates in LDAP
weijun
parents: 47216
diff changeset
   141
            security.checkConnect(serverName, port);
2ce508de5c77 8178458: Better use of certificates in LDAP
weijun
parents: 47216
diff changeset
   142
        }
2ce508de5c77 8178458: Better use of certificates in LDAP
weijun
parents: 47216
diff changeset
   143
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   144
        Key k = new Key(serverName, port);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   145
        LDAPCertStoreImpl lci = certStoreCache.get(k);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   146
        if (lci == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   147
            this.impl = new LDAPCertStoreImpl(serverName, port);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   148
            certStoreCache.put(k, impl);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   149
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   150
            this.impl = lci;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   151
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   152
                debug.println("LDAPCertStore.getInstance: cache hit");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   153
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   154
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   155
        this.ldapDN = dn;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   156
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   158
    private static class Key {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   159
        volatile int hashCode;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   160
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   161
        String serverName;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   162
        int port;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   164
        Key(String serverName, int port) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   165
            this.serverName = serverName;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   166
            this.port = port;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   167
        }
22089
ba496138fa35 8030813: Signed applet fails to load when CRLs are stored in an LDAP directory
mullan
parents: 10785
diff changeset
   168
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   169
        @Override
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   170
        public boolean equals(Object obj) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   171
            if (!(obj instanceof Key)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   172
                return false;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   173
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   174
            Key key = (Key) obj;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   175
            return (port == key.port &&
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   176
                serverName.equalsIgnoreCase(key.serverName));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   177
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   178
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   179
        @Override
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   180
        public int hashCode() {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   181
            if (hashCode == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   182
                int result = 17;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   183
                result = 37*result + port;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   184
                result = 37*result +
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   185
                    serverName.toLowerCase(Locale.ENGLISH).hashCode();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   186
                hashCode = result;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   187
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   188
            return hashCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   193
     * Returns an LDAPCertStoreImpl object. This method consults a cache of
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   194
     * LDAPCertStoreImpl objects (shared per JVM) using the corresponding
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   195
     * LDAP server name and port info as a key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   197
    private static final Cache<Key, LDAPCertStoreImpl>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   198
        certStoreCache = Cache.newSoftMemoryCache(185);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   200
    // Exist solely for regression test for ensuring that caching is done
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   201
    static synchronized LDAPCertStoreImpl getInstance(LDAPCertStoreParameters params)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   203
        String serverName = params.getServerName();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   204
        int port = params.getPort();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   205
        Key k = new Key(serverName, port);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   206
        LDAPCertStoreImpl lci = certStoreCache.get(k);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   207
        if (lci == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   208
            lci = new LDAPCertStoreImpl(serverName, port);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   209
            certStoreCache.put(k, lci);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                debug.println("LDAPCertStore.getInstance: cache hit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   215
        return lci;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Returns a <code>Collection</code> of <code>Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * match the specified selector. If no <code>Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * It is not practical to search every entry in the LDAP database for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * matching <code>Certificate</code>s. Instead, the <code>CertSelector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * is examined in order to determine where matching <code>Certificate</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * are likely to be found (according to the PKIX LDAPv2 schema, RFC 2587).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * If the subject is specified, its directory entry is searched. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * issuer is specified, its directory entry is searched. If neither the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * subject nor the issuer are specified (or the selector is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <code>X509CertSelector</code>), a <code>CertStoreException</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param selector a <code>CertSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *  <code>Certificate</code>s should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return a <code>Collection</code> of <code>Certificate</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *         match the specified selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
32502
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   239
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public synchronized Collection<X509Certificate> engineGetCertificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            (CertSelector selector) throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            debug.println("LDAPCertStore.engineGetCertificates() selector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                + String.valueOf(selector));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (selector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            selector = new X509CertSelector();
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   248
        } else if (!(selector instanceof X509CertSelector)) {
32502
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   249
            throw new CertStoreException("Need X509CertSelector to find certs, "
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   250
                    + "but instance of " + selector.getClass().getName()
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   251
                    + " passed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   253
        return impl.getCertificates((X509CertSelector) selector, ldapDN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Returns a <code>Collection</code> of <code>CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * match the specified selector. If no <code>CRL</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * match the selector, an empty <code>Collection</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * It is not practical to search every entry in the LDAP database for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * matching <code>CRL</code>s. Instead, the <code>CRLSelector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * is examined in order to determine where matching <code>CRL</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * are likely to be found (according to the PKIX LDAPv2 schema, RFC 2587).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * If issuerNames or certChecking are specified, the issuer's directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * entry is searched. If neither issuerNames or certChecking are specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * (or the selector is not an <code>X509CRLSelector</code>), a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <code>CertStoreException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param selector A <code>CRLSelector</code> used to select which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *  <code>CRL</code>s should be returned. Specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *  to return all <code>CRL</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return A <code>Collection</code> of <code>CRL</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *         match the specified selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @throws CertStoreException if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
32502
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   277
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public synchronized Collection<X509CRL> engineGetCRLs(CRLSelector selector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throws CertStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            debug.println("LDAPCertStore.engineGetCRLs() selector: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                + selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // Set up selector and collection to hold CRLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (selector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            selector = new X509CRLSelector();
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   287
        } else if (!(selector instanceof X509CRLSelector)) {
32502
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   288
            throw new CertStoreException("Need X509CRLSelector to find CRLs, "
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   289
                    + "but instance of " + selector.getClass().getName()
03f7450aec42 8134708: Certpath validation fails to load certs and CRLs if AIA and CRLDP extensions point to LDAP resources
asmotrak
parents: 30506
diff changeset
   290
                    + " passed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents: 28429
diff changeset
   292
        return impl.getCRLs((X509CRLSelector) selector, ldapDN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}