src/java.naming/share/classes/sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 55097 ae908641e726
permissions -rw-r--r--
8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.* Reviewed-by: rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
     1
/*
55097
ae908641e726 8224729: Cleanups in sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
clanger
parents: 48583
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
     4
 *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    10
 *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    15
 * accompanied this code).
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    16
 *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    20
 *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    23
 * questions.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    24
 */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    25
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    26
package sun.security.provider.certpath.ldap;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    27
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
    29
import java.net.URI;
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    30
import java.util.*;
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
    31
import javax.naming.CompositeName;
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    32
import javax.naming.Context;
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
    33
import javax.naming.InvalidNameException;
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    34
import javax.naming.NamingEnumeration;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    35
import javax.naming.NamingException;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    36
import javax.naming.NameNotFoundException;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    37
import javax.naming.directory.Attribute;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    38
import javax.naming.directory.Attributes;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    39
import javax.naming.directory.BasicAttributes;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    40
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    41
import java.security.*;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    42
import java.security.cert.Certificate;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    43
import java.security.cert.*;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    44
import javax.naming.CommunicationException;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    45
import javax.naming.ldap.InitialLdapContext;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    46
import javax.naming.ldap.LdapContext;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    47
import javax.security.auth.x500.X500Principal;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    48
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
    49
import com.sun.jndi.ldap.LdapReferralException;
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 30506
diff changeset
    50
import sun.security.util.HexDumpEncoder;
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    51
import sun.security.provider.certpath.X509CertificatePair;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    52
import sun.security.util.Cache;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    53
import sun.security.util.Debug;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    54
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    55
/**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    56
 * Core implementation of a LDAP Cert Store.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    57
 * @see java.security.cert.CertStore
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    58
 *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34687
diff changeset
    59
 * @since       9
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    60
 */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    61
final class LDAPCertStoreImpl {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    62
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    63
    private static final Debug debug = Debug.getInstance("certpath");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    64
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    65
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    66
     * LDAP attribute identifiers.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    67
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    68
    private static final String USER_CERT = "userCertificate;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    69
    private static final String CA_CERT = "cACertificate;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    70
    private static final String CROSS_CERT = "crossCertificatePair;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    71
    private static final String CRL = "certificateRevocationList;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    72
    private static final String ARL = "authorityRevocationList;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    73
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    74
    // Constants for various empty values
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    75
    private final static String[] STRING0 = new String[0];
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    76
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    77
    private final static byte[][] BB0 = new byte[0][];
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    78
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    79
    private final static Attributes EMPTY_ATTRIBUTES = new BasicAttributes();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    80
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    81
    // cache related constants
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    82
    private final static int DEFAULT_CACHE_SIZE = 750;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    83
    private final static int DEFAULT_CACHE_LIFETIME = 30;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    84
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    85
    private final static int LIFETIME;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    86
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    87
    private final static String PROP_LIFETIME =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    88
                            "sun.security.certpath.ldap.cache.lifetime";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    89
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    90
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    91
     * Internal system property, that when set to "true", disables the
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    92
     * JNDI application resource files lookup to prevent recursion issues
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    93
     * when validating signed JARs with LDAP URLs in certificates.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    94
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    95
    private final static String PROP_DISABLE_APP_RESOURCE_FILES =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    96
        "sun.security.certpath.ldap.disable.app.resource.files";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    97
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    98
    static {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    99
        String s = AccessController.doPrivileged(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   100
            (PrivilegedAction<String>) () -> System.getProperty(PROP_LIFETIME));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   101
        if (s != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   102
            LIFETIME = Integer.parseInt(s); // throws NumberFormatException
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   103
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   104
            LIFETIME = DEFAULT_CACHE_LIFETIME;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   105
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   106
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   107
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   108
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   109
     * The CertificateFactory used to decode certificates from
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   110
     * their binary stored form.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   111
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   112
    private CertificateFactory cf;
55097
ae908641e726 8224729: Cleanups in sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
clanger
parents: 48583
diff changeset
   113
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   114
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   115
     * The JNDI directory context.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   116
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   117
    private LdapContext ctx;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   118
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   119
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   120
     * Flag indicating that communication error occurred.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   121
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   122
    private boolean communicationError = false;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   123
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   124
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   125
     * Flag indicating whether we should prefetch CRLs.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   126
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   127
    private boolean prefetchCRLs = false;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   128
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   129
    private final Cache<String, byte[][]> valueCache;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   130
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   131
    private int cacheHits = 0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   132
    private int cacheMisses = 0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   133
    private int requests = 0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   134
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   135
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   136
     * Creates a <code>CertStore</code> with the specified parameters.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   137
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   138
    LDAPCertStoreImpl(String serverName, int port)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   139
        throws InvalidAlgorithmParameterException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   140
        createInitialDirContext(serverName, port);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   141
        // Create CertificateFactory for use later on
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   142
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   143
            cf = CertificateFactory.getInstance("X.509");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   144
        } catch (CertificateException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   145
            throw new InvalidAlgorithmParameterException(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   146
                "unable to create CertificateFactory for X.509");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   147
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   148
        if (LIFETIME == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   149
            valueCache = Cache.newNullCache();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   150
        } else if (LIFETIME < 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   151
            valueCache = Cache.newSoftMemoryCache(DEFAULT_CACHE_SIZE);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   152
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   153
            valueCache = Cache.newSoftMemoryCache(DEFAULT_CACHE_SIZE, LIFETIME);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   154
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   155
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   156
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   157
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   158
     * Create InitialDirContext.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   159
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   160
     * @param server Server DNS name hosting LDAP service
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   161
     * @param port   Port at which server listens for requests
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   162
     * @throws InvalidAlgorithmParameterException if creation fails
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   163
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   164
    private void createInitialDirContext(String server, int port)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   165
            throws InvalidAlgorithmParameterException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   166
        String url = "ldap://" + server + ":" + port;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   167
        Hashtable<String,Object> env = new Hashtable<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   168
        env.put(Context.INITIAL_CONTEXT_FACTORY,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   169
                "com.sun.jndi.ldap.LdapCtxFactory");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   170
        env.put(Context.PROVIDER_URL, url);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   171
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   172
        // If property is set to true, disable application resource file lookup.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   173
        boolean disableAppResourceFiles = AccessController.doPrivileged(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   174
            (PrivilegedAction<Boolean>) () -> Boolean.getBoolean(PROP_DISABLE_APP_RESOURCE_FILES));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   175
        if (disableAppResourceFiles) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   176
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   177
                debug.println("LDAPCertStore disabling app resource files");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   178
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   179
            env.put("com.sun.naming.disable.app.resource.files", "true");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   180
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   181
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   182
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   183
            ctx = new InitialLdapContext(env, null);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   184
            /*
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   185
             * Always deal with referrals here.
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   186
             */
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   187
            ctx.addToEnvironment(Context.REFERRAL, "throw");
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   188
        } catch (NamingException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   189
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   190
                debug.println("LDAPCertStore.engineInit about to throw "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   191
                    + "InvalidAlgorithmParameterException");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   192
                e.printStackTrace();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   193
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   194
            Exception ee = new InvalidAlgorithmParameterException
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   195
                ("unable to create InitialDirContext using supplied parameters");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   196
            ee.initCause(e);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   197
            throw (InvalidAlgorithmParameterException)ee;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   198
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   199
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   200
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   201
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   202
     * Private class encapsulating the actual LDAP operations and cache
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   203
     * handling. Use:
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   204
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   205
     *   LDAPRequest request = new LDAPRequest(dn);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   206
     *   request.addRequestedAttribute(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   207
     *   request.addRequestedAttribute(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   208
     *   byte[][] crossValues = request.getValues(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   209
     *   byte[][] caValues = request.getValues(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   210
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   211
     * At most one LDAP request is sent for each instance created. If all
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   212
     * getValues() calls can be satisfied from the cache, no request
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   213
     * is sent at all. If a request is sent, all requested attributes
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   214
     * are always added to the cache irrespective of whether the getValues()
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   215
     * method is called.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   216
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   217
    private class LDAPRequest {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   218
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   219
        private final String name;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   220
        private Map<String, byte[][]> valueMap;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   221
        private final List<String> requestedAttributes;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   222
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   223
        LDAPRequest(String name) throws CertStoreException {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   224
            this.name = checkName(name);
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   225
            requestedAttributes = new ArrayList<>(5);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   226
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   227
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   228
        private String checkName(String name) throws CertStoreException {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   229
            if (name == null) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   230
                throw new CertStoreException("Name absent");
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   231
            }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   232
            try {
48583
02cc6b9c271d 8190789: sun/security/provider/certpath/LDAPCertStore/TestURICertStoreParameters.java fails after JDK-8186606
weijun
parents: 48582
diff changeset
   233
                if (new CompositeName(name).size() > 1) {
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   234
                    throw new CertStoreException("Invalid name: " + name);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   235
                }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   236
            } catch (InvalidNameException ine) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   237
                throw new CertStoreException("Invalid name: " + name, ine);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   238
            }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   239
            return name;
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   240
        }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   241
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   242
        void addRequestedAttribute(String attrId) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   243
            if (valueMap != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   244
                throw new IllegalStateException("Request already sent");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   245
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   246
            requestedAttributes.add(attrId);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   247
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   248
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   249
        /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   250
         * Gets one or more binary values from an attribute.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   251
         *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   252
         * @param attrId                the attribute identifier
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   253
         * @return                      an array of binary values (byte arrays)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   254
         * @throws NamingException      if a naming exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   255
         */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   256
        byte[][] getValues(String attrId) throws NamingException {
55097
ae908641e726 8224729: Cleanups in sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
clanger
parents: 48583
diff changeset
   257
            if (debug != null && Debug.isVerbose() && ((cacheHits + cacheMisses) % 50 == 0)) {
ae908641e726 8224729: Cleanups in sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
clanger
parents: 48583
diff changeset
   258
                debug.println("LDAPRequest Cache hits: " + cacheHits +
ae908641e726 8224729: Cleanups in sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
clanger
parents: 48583
diff changeset
   259
                    "; misses: " + cacheMisses);
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   260
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   261
            String cacheKey = name + "|" + attrId;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   262
            byte[][] values = valueCache.get(cacheKey);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   263
            if (values != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   264
                cacheHits++;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   265
                return values;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   266
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   267
            cacheMisses++;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   268
            Map<String, byte[][]> attrs = getValueMap();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   269
            values = attrs.get(attrId);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   270
            return values;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   271
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   272
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   273
        /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   274
         * Get a map containing the values for this request. The first time
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   275
         * this method is called on an object, the LDAP request is sent,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   276
         * the results parsed and added to a private map and also to the
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   277
         * cache of this LDAPCertStore. Subsequent calls return the private
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   278
         * map immediately.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   279
         *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   280
         * The map contains an entry for each requested attribute. The
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   281
         * attribute name is the key, values are byte[][]. If there are no
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   282
         * values for that attribute, values are byte[0][].
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   283
         *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   284
         * @return                      the value Map
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   285
         * @throws NamingException      if a naming exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   286
         */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   287
        private Map<String, byte[][]> getValueMap() throws NamingException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   288
            if (valueMap != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   289
                return valueMap;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   290
            }
55097
ae908641e726 8224729: Cleanups in sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
clanger
parents: 48583
diff changeset
   291
            if (debug != null && Debug.isVerbose()) {
ae908641e726 8224729: Cleanups in sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
clanger
parents: 48583
diff changeset
   292
                debug.println("LDAPRequest: " + name + ":" + requestedAttributes);
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   293
                requests++;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   294
                if (requests % 5 == 0) {
55097
ae908641e726 8224729: Cleanups in sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
clanger
parents: 48583
diff changeset
   295
                    debug.println("LDAP requests: " + requests);
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   296
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   297
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   298
            valueMap = new HashMap<>(8);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   299
            String[] attrIds = requestedAttributes.toArray(STRING0);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   300
            Attributes attrs;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   301
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   302
            if (communicationError) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   303
                ctx.reconnect(null);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   304
                communicationError = false;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   305
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   306
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   307
            try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   308
                attrs = ctx.getAttributes(name, attrIds);
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   309
            } catch (LdapReferralException lre) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   310
                // LdapCtx has a hopCount field to avoid infinite loop
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   311
                while (true) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   312
                    try {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   313
                        String newName = (String) lre.getReferralInfo();
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   314
                        URI newUri = new URI(newName);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   315
                        if (!newUri.getScheme().equalsIgnoreCase("ldap")) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   316
                            throw new IllegalArgumentException("Not LDAP");
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   317
                        }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   318
                        String newDn = newUri.getPath();
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   319
                        if (newDn != null && newDn.charAt(0) == '/') {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   320
                            newDn = newDn.substring(1);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   321
                        }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   322
                        checkName(newDn);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   323
                    } catch (Exception e) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   324
                        throw new NamingException("Cannot follow referral to "
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   325
                                + lre.getReferralInfo());
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   326
                    }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   327
                    LdapContext refCtx =
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   328
                            (LdapContext)lre.getReferralContext();
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   329
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   330
                    // repeat the original operation at the new context
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   331
                    try {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   332
                        attrs = refCtx.getAttributes(name, attrIds);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   333
                        break;
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   334
                    } catch (LdapReferralException re) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   335
                        lre = re;
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   336
                        continue;
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   337
                    } finally {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   338
                        // Make sure we close referral context
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   339
                        refCtx.close();
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   340
                    }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   341
                }
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   342
            } catch (CommunicationException ce) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   343
                communicationError = true;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   344
                throw ce;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   345
            } catch (NameNotFoundException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   346
                // name does not exist on this LDAP server
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   347
                // treat same as not attributes found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   348
                attrs = EMPTY_ATTRIBUTES;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   349
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   350
            for (String attrId : requestedAttributes) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   351
                Attribute attr = attrs.get(attrId);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   352
                byte[][] values = getAttributeValues(attr);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   353
                cacheAttribute(attrId, values);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   354
                valueMap.put(attrId, values);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   355
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   356
            return valueMap;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   357
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   358
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   359
        /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   360
         * Add the values to the cache.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   361
         */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   362
        private void cacheAttribute(String attrId, byte[][] values) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   363
            String cacheKey = name + "|" + attrId;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   364
            valueCache.put(cacheKey, values);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   365
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   366
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   367
        /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   368
         * Get the values for the given attribute. If the attribute is null
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   369
         * or does not contain any values, a zero length byte array is
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   370
         * returned. NOTE that it is assumed that all values are byte arrays.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   371
         */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   372
        private byte[][] getAttributeValues(Attribute attr)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   373
                throws NamingException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   374
            byte[][] values;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   375
            if (attr == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   376
                values = BB0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   377
            } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   378
                values = new byte[attr.size()][];
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   379
                int i = 0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   380
                NamingEnumeration<?> enum_ = attr.getAll();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   381
                while (enum_.hasMore()) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   382
                    Object obj = enum_.next();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   383
                    if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   384
                        if (obj instanceof String) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   385
                            debug.println("LDAPCertStore.getAttrValues() "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   386
                                + "enum.next is a string!: " + obj);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   387
                        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   388
                    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   389
                    byte[] value = (byte[])obj;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   390
                    values[i++] = value;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   391
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   392
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   393
            return values;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   394
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   395
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   396
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   397
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   398
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   399
     * Gets certificates from an attribute id and location in the LDAP
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   400
     * directory. Returns a Collection containing only the Certificates that
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   401
     * match the specified CertSelector.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   402
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   403
     * @param name the location holding the attribute
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   404
     * @param id the attribute identifier
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   405
     * @param sel a CertSelector that the Certificates must match
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   406
     * @return a Collection of Certificates found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   407
     * @throws CertStoreException       if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   408
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   409
    private Collection<X509Certificate> getCertificates(LDAPRequest request,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   410
        String id, X509CertSelector sel) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   411
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   412
        /* fetch encoded certs from storage */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   413
        byte[][] encodedCert;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   414
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   415
            encodedCert = request.getValues(id);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   416
        } catch (NamingException namingEx) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   417
            throw new CertStoreException(namingEx);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   418
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   419
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   420
        int n = encodedCert.length;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   421
        if (n == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   422
            return Collections.emptySet();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   423
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   424
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   425
        List<X509Certificate> certs = new ArrayList<>(n);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   426
        /* decode certs and check if they satisfy selector */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   427
        for (int i = 0; i < n; i++) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   428
            ByteArrayInputStream bais = new ByteArrayInputStream(encodedCert[i]);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   429
            try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   430
                Certificate cert = cf.generateCertificate(bais);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   431
                if (sel.match(cert)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   432
                  certs.add((X509Certificate)cert);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   433
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   434
            } catch (CertificateException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   435
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   436
                    debug.println("LDAPCertStore.getCertificates() encountered "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   437
                        + "exception while parsing cert, skipping the bad data: ");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   438
                    HexDumpEncoder encoder = new HexDumpEncoder();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   439
                    debug.println(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   440
                        "[ " + encoder.encodeBuffer(encodedCert[i]) + " ]");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   441
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   442
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   443
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   444
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   445
        return certs;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   446
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   447
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   448
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   449
     * Gets certificate pairs from an attribute id and location in the LDAP
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   450
     * directory.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   451
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   452
     * @param name the location holding the attribute
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   453
     * @param id the attribute identifier
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   454
     * @return a Collection of X509CertificatePairs found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   455
     * @throws CertStoreException       if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   456
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   457
    private Collection<X509CertificatePair> getCertPairs(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   458
        LDAPRequest request, String id) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   459
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   460
        /* fetch the encoded cert pairs from storage */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   461
        byte[][] encodedCertPair;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   462
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   463
            encodedCertPair = request.getValues(id);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   464
        } catch (NamingException namingEx) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   465
            throw new CertStoreException(namingEx);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   466
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   467
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   468
        int n = encodedCertPair.length;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   469
        if (n == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   470
            return Collections.emptySet();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   471
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   472
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   473
        List<X509CertificatePair> certPairs = new ArrayList<>(n);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   474
        /* decode each cert pair and add it to the Collection */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   475
        for (int i = 0; i < n; i++) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   476
            try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   477
                X509CertificatePair certPair =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   478
                    X509CertificatePair.generateCertificatePair(encodedCertPair[i]);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   479
                certPairs.add(certPair);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   480
            } catch (CertificateException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   481
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   482
                    debug.println(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   483
                        "LDAPCertStore.getCertPairs() encountered exception "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   484
                        + "while parsing cert, skipping the bad data: ");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   485
                    HexDumpEncoder encoder = new HexDumpEncoder();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   486
                    debug.println(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   487
                        "[ " + encoder.encodeBuffer(encodedCertPair[i]) + " ]");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   488
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   489
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   490
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   491
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   492
        return certPairs;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   493
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   494
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   495
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   496
     * Looks at certificate pairs stored in the crossCertificatePair attribute
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   497
     * at the specified location in the LDAP directory. Returns a Collection
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   498
     * containing all X509Certificates stored in the forward component that match
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   499
     * the forward X509CertSelector and all Certificates stored in the reverse
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   500
     * component that match the reverse X509CertSelector.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   501
     * <p>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   502
     * If either forward or reverse is null, all certificates from the
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   503
     * corresponding component will be rejected.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   504
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   505
     * @param name the location to look in
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   506
     * @param forward the forward X509CertSelector (or null)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   507
     * @param reverse the reverse X509CertSelector (or null)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   508
     * @return a Collection of X509Certificates found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   509
     * @throws CertStoreException       if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   510
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   511
    private Collection<X509Certificate> getMatchingCrossCerts(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   512
            LDAPRequest request, X509CertSelector forward,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   513
            X509CertSelector reverse)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   514
            throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   515
        // Get the cert pairs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   516
        Collection<X509CertificatePair> certPairs =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   517
                                getCertPairs(request, CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   518
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   519
        // Find Certificates that match and put them in a list
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   520
        ArrayList<X509Certificate> matchingCerts = new ArrayList<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   521
        for (X509CertificatePair certPair : certPairs) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   522
            X509Certificate cert;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   523
            if (forward != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   524
                cert = certPair.getForward();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   525
                if ((cert != null) && forward.match(cert)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   526
                    matchingCerts.add(cert);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   527
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   528
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   529
            if (reverse != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   530
                cert = certPair.getReverse();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   531
                if ((cert != null) && reverse.match(cert)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   532
                    matchingCerts.add(cert);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   533
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   534
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   535
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   536
        return matchingCerts;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   537
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   538
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   539
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   540
     * Returns a <code>Collection</code> of <code>X509Certificate</code>s that
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   541
     * match the specified selector. If no <code>X509Certificate</code>s
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   542
     * match the selector, an empty <code>Collection</code> will be returned.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   543
     * <p>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   544
     * It is not practical to search every entry in the LDAP database for
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   545
     * matching <code>X509Certificate</code>s. Instead, the
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   546
     * <code>X509CertSelector</code> is examined in order to determine where
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   547
     * matching <code>Certificate</code>s are likely to be found (according
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   548
     * to the PKIX LDAPv2 schema, RFC 2587).
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   549
     * If the subject is specified, its directory entry is searched. If the
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   550
     * issuer is specified, its directory entry is searched. If neither the
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   551
     * subject nor the issuer are specified (or the selector is not an
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   552
     * <code>X509CertSelector</code>), a <code>CertStoreException</code> is
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   553
     * thrown.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   554
     *
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   555
     * @param xsel a <code>X509CertSelector</code> used to select which
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   556
     *  <code>Certificate</code>s should be returned.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   557
     * @return a <code>Collection</code> of <code>X509Certificate</code>s that
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   558
     *         match the specified selector
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   559
     * @throws CertStoreException if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   560
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   561
    synchronized Collection<X509Certificate> getCertificates
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   562
        (X509CertSelector xsel, String ldapDN) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   563
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   564
        if (ldapDN == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   565
            ldapDN = xsel.getSubjectAsString();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   566
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   567
        int basicConstraints = xsel.getBasicConstraints();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   568
        String issuer = xsel.getIssuerAsString();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   569
        HashSet<X509Certificate> certs = new HashSet<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   570
        if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   571
            debug.println("LDAPCertStore.engineGetCertificates() basicConstraints: "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   572
                + basicConstraints);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   573
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   574
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   575
        // basicConstraints:
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   576
        // -2: only EE certs accepted
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   577
        // -1: no check is done
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   578
        //  0: any CA certificate accepted
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   579
        // >1: certificate's basicConstraints extension pathlen must match
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   580
        if (ldapDN != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   581
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   582
                debug.println("LDAPCertStore.engineGetCertificates() "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   583
                    + " subject is not null");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   584
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   585
            LDAPRequest request = new LDAPRequest(ldapDN);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   586
            if (basicConstraints > -2) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   587
                request.addRequestedAttribute(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   588
                request.addRequestedAttribute(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   589
                request.addRequestedAttribute(ARL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   590
                if (prefetchCRLs) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   591
                    request.addRequestedAttribute(CRL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   592
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   593
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   594
            if (basicConstraints < 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   595
                request.addRequestedAttribute(USER_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   596
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   597
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   598
            if (basicConstraints > -2) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   599
                certs.addAll(getMatchingCrossCerts(request, xsel, null));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   600
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   601
                    debug.println("LDAPCertStore.engineGetCertificates() after "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   602
                        + "getMatchingCrossCerts(subject,xsel,null),certs.size(): "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   603
                        + certs.size());
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   604
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   605
                certs.addAll(getCertificates(request, CA_CERT, xsel));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   606
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   607
                    debug.println("LDAPCertStore.engineGetCertificates() after "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   608
                        + "getCertificates(subject,CA_CERT,xsel),certs.size(): "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   609
                        + certs.size());
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   610
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   611
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   612
            if (basicConstraints < 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   613
                certs.addAll(getCertificates(request, USER_CERT, xsel));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   614
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   615
                    debug.println("LDAPCertStore.engineGetCertificates() after "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   616
                        + "getCertificates(subject,USER_CERT, xsel),certs.size(): "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   617
                        + certs.size());
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   618
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   619
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   620
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   621
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   622
                debug.println
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   623
                    ("LDAPCertStore.engineGetCertificates() subject is null");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   624
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   625
            if (basicConstraints == -2) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   626
                throw new CertStoreException("need subject to find EE certs");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   627
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   628
            if (issuer == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   629
                throw new CertStoreException("need subject or issuer to find certs");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   630
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   631
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   632
        if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   633
            debug.println("LDAPCertStore.engineGetCertificates() about to "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   634
                + "getMatchingCrossCerts...");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   635
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   636
        if ((issuer != null) && (basicConstraints > -2)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   637
            LDAPRequest request = new LDAPRequest(issuer);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   638
            request.addRequestedAttribute(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   639
            request.addRequestedAttribute(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   640
            request.addRequestedAttribute(ARL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   641
            if (prefetchCRLs) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   642
                request.addRequestedAttribute(CRL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   643
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   644
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   645
            certs.addAll(getMatchingCrossCerts(request, null, xsel));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   646
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   647
                debug.println("LDAPCertStore.engineGetCertificates() after "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   648
                    + "getMatchingCrossCerts(issuer,null,xsel),certs.size(): "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   649
                    + certs.size());
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   650
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   651
            certs.addAll(getCertificates(request, CA_CERT, xsel));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   652
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   653
                debug.println("LDAPCertStore.engineGetCertificates() after "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   654
                    + "getCertificates(issuer,CA_CERT,xsel),certs.size(): "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   655
                    + certs.size());
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   656
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   657
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   658
        if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   659
            debug.println("LDAPCertStore.engineGetCertificates() returning certs");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   660
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   661
        return certs;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   662
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   663
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   664
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   665
     * Gets CRLs from an attribute id and location in the LDAP directory.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   666
     * Returns a Collection containing only the CRLs that match the
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   667
     * specified X509CRLSelector.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   668
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   669
     * @param name the location holding the attribute
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   670
     * @param id the attribute identifier
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   671
     * @param sel a X509CRLSelector that the CRLs must match
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   672
     * @return a Collection of CRLs found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   673
     * @throws CertStoreException       if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   674
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   675
    private Collection<X509CRL> getCRLs(LDAPRequest request, String id,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   676
            X509CRLSelector sel) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   677
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   678
        /* fetch the encoded crls from storage */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   679
        byte[][] encodedCRL;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   680
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   681
            encodedCRL = request.getValues(id);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   682
        } catch (NamingException namingEx) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   683
            throw new CertStoreException(namingEx);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   684
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   685
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   686
        int n = encodedCRL.length;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   687
        if (n == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   688
            return Collections.emptySet();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   689
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   690
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   691
        List<X509CRL> crls = new ArrayList<>(n);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   692
        /* decode each crl and check if it matches selector */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   693
        for (int i = 0; i < n; i++) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   694
            try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   695
                CRL crl = cf.generateCRL(new ByteArrayInputStream(encodedCRL[i]));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   696
                if (sel.match(crl)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   697
                    crls.add((X509CRL)crl);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   698
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   699
            } catch (CRLException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   700
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   701
                    debug.println("LDAPCertStore.getCRLs() encountered exception"
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   702
                        + " while parsing CRL, skipping the bad data: ");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   703
                    HexDumpEncoder encoder = new HexDumpEncoder();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   704
                    debug.println("[ " + encoder.encodeBuffer(encodedCRL[i]) + " ]");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   705
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   706
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   707
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   708
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   709
        return crls;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   710
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   711
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   712
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   713
     * Returns a <code>Collection</code> of <code>X509CRL</code>s that
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   714
     * match the specified selector. If no <code>X509CRL</code>s
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   715
     * match the selector, an empty <code>Collection</code> will be returned.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   716
     * <p>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   717
     * It is not practical to search every entry in the LDAP database for
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   718
     * matching <code>X509CRL</code>s. Instead, the <code>X509CRLSelector</code>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   719
     * is examined in order to determine where matching <code>X509CRL</code>s
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   720
     * are likely to be found (according to the PKIX LDAPv2 schema, RFC 2587).
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   721
     * If issuerNames or certChecking are specified, the issuer's directory
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   722
     * entry is searched. If neither issuerNames or certChecking are specified
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   723
     * (or the selector is not an <code>X509CRLSelector</code>), a
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   724
     * <code>CertStoreException</code> is thrown.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   725
     *
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   726
     * @param xsel A <code>X509CRLSelector</code> used to select which
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   727
     *  <code>CRL</code>s should be returned. Specify <code>null</code>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   728
     *  to return all <code>CRL</code>s.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   729
     * @return A <code>Collection</code> of <code>X509CRL</code>s that
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   730
     *         match the specified selector
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   731
     * @throws CertStoreException if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   732
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   733
    synchronized Collection<X509CRL> getCRLs(X509CRLSelector xsel,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   734
         String ldapDN) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   735
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   736
        HashSet<X509CRL> crls = new HashSet<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   737
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   738
        // Look in directory entry for issuer of cert we're checking.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   739
        Collection<Object> issuerNames;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   740
        X509Certificate certChecking = xsel.getCertificateChecking();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   741
        if (certChecking != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   742
            issuerNames = new HashSet<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   743
            X500Principal issuer = certChecking.getIssuerX500Principal();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   744
            issuerNames.add(issuer.getName(X500Principal.RFC2253));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   745
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   746
            // But if we don't know which cert we're checking, try the directory
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   747
            // entries of all acceptable CRL issuers
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   748
            if (ldapDN != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   749
                issuerNames = new HashSet<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   750
                issuerNames.add(ldapDN);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   751
            } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   752
                issuerNames = xsel.getIssuerNames();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   753
                if (issuerNames == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   754
                    throw new CertStoreException("need issuerNames or"
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   755
                       + " certChecking to find CRLs");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   756
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   757
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   758
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   759
        for (Object nameObject : issuerNames) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   760
            String issuerName;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   761
            if (nameObject instanceof byte[]) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   762
                try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   763
                    X500Principal issuer = new X500Principal((byte[])nameObject);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   764
                    issuerName = issuer.getName(X500Principal.RFC2253);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   765
                } catch (IllegalArgumentException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   766
                    continue;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   767
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   768
            } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   769
                issuerName = (String)nameObject;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   770
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   771
            // If all we want is CA certs, try to get the (probably shorter) ARL
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   772
            Collection<X509CRL> entryCRLs = Collections.emptySet();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   773
            if (certChecking == null || certChecking.getBasicConstraints() != -1) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   774
                LDAPRequest request = new LDAPRequest(issuerName);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   775
                request.addRequestedAttribute(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   776
                request.addRequestedAttribute(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   777
                request.addRequestedAttribute(ARL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   778
                if (prefetchCRLs) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   779
                    request.addRequestedAttribute(CRL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   780
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   781
                try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   782
                    entryCRLs = getCRLs(request, ARL, xsel);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   783
                    if (entryCRLs.isEmpty()) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   784
                        // no ARLs found. We assume that means that there are
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   785
                        // no ARLs on this server at all and prefetch the CRLs.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   786
                        prefetchCRLs = true;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   787
                    } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   788
                        crls.addAll(entryCRLs);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   789
                    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   790
                } catch (CertStoreException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   791
                    if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   792
                        debug.println("LDAPCertStore.engineGetCRLs non-fatal error "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   793
                            + "retrieving ARLs:" + e);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   794
                        e.printStackTrace();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   795
                    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   796
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   797
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   798
            // Otherwise, get the CRL
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   799
            // if certChecking is null, we don't know if we should look in ARL or CRL
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   800
            // attribute, so check both for matching CRLs.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   801
            if (entryCRLs.isEmpty() || certChecking == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   802
                LDAPRequest request = new LDAPRequest(issuerName);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   803
                request.addRequestedAttribute(CRL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   804
                entryCRLs = getCRLs(request, CRL, xsel);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   805
                crls.addAll(entryCRLs);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   806
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   807
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   808
        return crls;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   809
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   810
}