src/java.naming/share/classes/sun/security/provider/certpath/ldap/LDAPCertStoreImpl.java
author sangheki
Mon, 04 Jun 2018 21:20:16 -0700
changeset 50396 7f48bff40a9a
parent 48583 02cc6b9c271d
child 55097 ae908641e726
permissions -rw-r--r--
8204094: assert(worker_i < _length) failed: Worker 15 is greater than max: 11 at ReferenceProcessorPhaseTimes Reviewed-by: kbarrett, tschatzl
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
/*
45987
1fac9185cc2d 8176067: Proper directory lookup processing
weijun
parents: 36511
diff changeset
     2
 * Copyright (c) 2015, 2017, 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
    private final static boolean DEBUG = false;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    66
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
     * LDAP attribute identifiers.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    69
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    70
    private static final String USER_CERT = "userCertificate;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    71
    private static final String CA_CERT = "cACertificate;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    72
    private static final String CROSS_CERT = "crossCertificatePair;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    73
    private static final String CRL = "certificateRevocationList;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    74
    private static final String ARL = "authorityRevocationList;binary";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    75
    private static final String DELTA_CRL = "deltaRevocationList;binary";
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
    // Constants for various empty values
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    78
    private final static String[] STRING0 = new String[0];
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    79
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    80
    private final static byte[][] BB0 = new byte[0][];
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    81
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    82
    private final static Attributes EMPTY_ATTRIBUTES = new BasicAttributes();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    83
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    84
    // cache related constants
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    85
    private final static int DEFAULT_CACHE_SIZE = 750;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    86
    private final static int DEFAULT_CACHE_LIFETIME = 30;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    87
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    88
    private final static int 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
    private final static String PROP_LIFETIME =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    91
                            "sun.security.certpath.ldap.cache.lifetime";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    92
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    93
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    94
     * 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
    95
     * JNDI application resource files lookup to prevent recursion issues
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    96
     * when validating signed JARs with LDAP URLs in certificates.
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
    private final static String PROP_DISABLE_APP_RESOURCE_FILES =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
    99
        "sun.security.certpath.ldap.disable.app.resource.files";
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   100
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   101
    static {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   102
        String s = AccessController.doPrivileged(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   103
            (PrivilegedAction<String>) () -> System.getProperty(PROP_LIFETIME));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   104
        if (s != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   105
            LIFETIME = Integer.parseInt(s); // throws NumberFormatException
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   106
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   107
            LIFETIME = DEFAULT_CACHE_LIFETIME;
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
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   110
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
     * The CertificateFactory used to decode certificates from
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   113
     * their binary stored form.
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
    private CertificateFactory cf;
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
     * The JNDI directory context.
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
    private LdapContext ctx;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   120
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
     * Flag indicating that communication error occurred.
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
    private boolean communicationError = false;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   125
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
     * Flag indicating whether we should prefetch CRLs.
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 boolean prefetchCRLs = false;
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 final Cache<String, byte[][]> valueCache;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   132
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   133
    private int cacheHits = 0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   134
    private int cacheMisses = 0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   135
    private int requests = 0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   136
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
     * Creates a <code>CertStore</code> with the specified parameters.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   139
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   140
    LDAPCertStoreImpl(String serverName, int port)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   141
        throws InvalidAlgorithmParameterException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   142
        createInitialDirContext(serverName, port);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   143
        // Create CertificateFactory for use later on
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   144
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   145
            cf = CertificateFactory.getInstance("X.509");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   146
        } catch (CertificateException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   147
            throw new InvalidAlgorithmParameterException(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   148
                "unable to create CertificateFactory for X.509");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   149
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   150
        if (LIFETIME == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   151
            valueCache = Cache.newNullCache();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   152
        } else if (LIFETIME < 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   153
            valueCache = Cache.newSoftMemoryCache(DEFAULT_CACHE_SIZE);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   154
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   155
            valueCache = Cache.newSoftMemoryCache(DEFAULT_CACHE_SIZE, LIFETIME);
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
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
     * Create InitialDirContext.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   161
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   162
     * @param server Server DNS name hosting LDAP service
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   163
     * @param port   Port at which server listens for requests
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   164
     * @throws InvalidAlgorithmParameterException if creation fails
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   165
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   166
    private void createInitialDirContext(String server, int port)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   167
            throws InvalidAlgorithmParameterException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   168
        String url = "ldap://" + server + ":" + port;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   169
        Hashtable<String,Object> env = new Hashtable<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   170
        env.put(Context.INITIAL_CONTEXT_FACTORY,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   171
                "com.sun.jndi.ldap.LdapCtxFactory");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   172
        env.put(Context.PROVIDER_URL, url);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   173
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   174
        // 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
   175
        boolean disableAppResourceFiles = AccessController.doPrivileged(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   176
            (PrivilegedAction<Boolean>) () -> Boolean.getBoolean(PROP_DISABLE_APP_RESOURCE_FILES));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   177
        if (disableAppResourceFiles) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   178
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   179
                debug.println("LDAPCertStore disabling app resource files");
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
            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
   182
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   183
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   184
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   185
            ctx = new InitialLdapContext(env, null);
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
             * Always deal with referrals here.
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   188
             */
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   189
            ctx.addToEnvironment(Context.REFERRAL, "throw");
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   190
        } catch (NamingException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   191
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   192
                debug.println("LDAPCertStore.engineInit about to throw "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   193
                    + "InvalidAlgorithmParameterException");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   194
                e.printStackTrace();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   195
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   196
            Exception ee = new InvalidAlgorithmParameterException
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   197
                ("unable to create InitialDirContext using supplied parameters");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   198
            ee.initCause(e);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   199
            throw (InvalidAlgorithmParameterException)ee;
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
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   203
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   204
     * Private class encapsulating the actual LDAP operations and cache
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   205
     * handling. Use:
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   206
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   207
     *   LDAPRequest request = new LDAPRequest(dn);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   208
     *   request.addRequestedAttribute(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   209
     *   request.addRequestedAttribute(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   210
     *   byte[][] crossValues = request.getValues(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   211
     *   byte[][] caValues = request.getValues(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   212
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   213
     * 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
   214
     * 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
   215
     * 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
   216
     * 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
   217
     * method is called.
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 class LDAPRequest {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   220
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   221
        private final String name;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   222
        private Map<String, byte[][]> valueMap;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   223
        private final List<String> requestedAttributes;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   224
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   225
        LDAPRequest(String name) throws CertStoreException {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   226
            this.name = checkName(name);
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   227
            requestedAttributes = new ArrayList<>(5);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   228
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   229
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   230
        private String checkName(String name) throws CertStoreException {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   231
            if (name == null) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   232
                throw new CertStoreException("Name absent");
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   233
            }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   234
            try {
48583
02cc6b9c271d 8190789: sun/security/provider/certpath/LDAPCertStore/TestURICertStoreParameters.java fails after JDK-8186606
weijun
parents: 48582
diff changeset
   235
                if (new CompositeName(name).size() > 1) {
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   236
                    throw new CertStoreException("Invalid name: " + name);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   237
                }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   238
            } catch (InvalidNameException ine) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   239
                throw new CertStoreException("Invalid name: " + name, ine);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   240
            }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   241
            return name;
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   242
        }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   243
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   244
        String getName() {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   245
            return name;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   246
        }
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
        void addRequestedAttribute(String attrId) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   249
            if (valueMap != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   250
                throw new IllegalStateException("Request already sent");
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
            requestedAttributes.add(attrId);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   253
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   254
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
         * Gets one or more binary values from an attribute.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   257
         *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   258
         * @param attrId                the attribute identifier
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   259
         * @return                      an array of binary values (byte arrays)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   260
         * @throws NamingException      if a naming exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   261
         */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   262
        byte[][] getValues(String attrId) throws NamingException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   263
            if (DEBUG && ((cacheHits + cacheMisses) % 50 == 0)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   264
                System.out.println("Cache hits: " + cacheHits + "; misses: "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   265
                        + cacheMisses);
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
            String cacheKey = name + "|" + attrId;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   268
            byte[][] values = valueCache.get(cacheKey);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   269
            if (values != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   270
                cacheHits++;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   271
                return values;
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
            cacheMisses++;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   274
            Map<String, byte[][]> attrs = getValueMap();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   275
            values = attrs.get(attrId);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   276
            return values;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   277
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   278
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
         * 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
   281
         * 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
   282
         * 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
   283
         * cache of this LDAPCertStore. Subsequent calls return the private
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   284
         * map immediately.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   285
         *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   286
         * 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
   287
         * 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
   288
         * values for that attribute, values are byte[0][].
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   289
         *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   290
         * @return                      the value Map
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   291
         * @throws NamingException      if a naming exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   292
         */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   293
        private Map<String, byte[][]> getValueMap() throws NamingException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   294
            if (valueMap != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   295
                return valueMap;
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
            if (DEBUG) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   298
                System.out.println("Request: " + name + ":" + requestedAttributes);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   299
                requests++;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   300
                if (requests % 5 == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   301
                    System.out.println("LDAP requests: " + requests);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   302
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   303
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   304
            valueMap = new HashMap<>(8);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   305
            String[] attrIds = requestedAttributes.toArray(STRING0);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   306
            Attributes attrs;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   307
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   308
            if (communicationError) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   309
                ctx.reconnect(null);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   310
                communicationError = false;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   311
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   312
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   313
            try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   314
                attrs = ctx.getAttributes(name, attrIds);
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   315
            } catch (LdapReferralException lre) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   316
                // LdapCtx has a hopCount field to avoid infinite loop
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   317
                while (true) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   318
                    try {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   319
                        String newName = (String) lre.getReferralInfo();
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   320
                        URI newUri = new URI(newName);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   321
                        if (!newUri.getScheme().equalsIgnoreCase("ldap")) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   322
                            throw new IllegalArgumentException("Not LDAP");
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   323
                        }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   324
                        String newDn = newUri.getPath();
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   325
                        if (newDn != null && newDn.charAt(0) == '/') {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   326
                            newDn = newDn.substring(1);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   327
                        }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   328
                        checkName(newDn);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   329
                    } catch (Exception e) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   330
                        throw new NamingException("Cannot follow referral to "
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   331
                                + lre.getReferralInfo());
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   332
                    }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   333
                    LdapContext refCtx =
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   334
                            (LdapContext)lre.getReferralContext();
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   335
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   336
                    // repeat the original operation at the new context
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   337
                    try {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   338
                        attrs = refCtx.getAttributes(name, attrIds);
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   339
                        break;
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   340
                    } catch (LdapReferralException re) {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   341
                        lre = re;
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   342
                        continue;
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   343
                    } finally {
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   344
                        // Make sure we close referral context
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   345
                        refCtx.close();
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   346
                    }
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   347
                }
30506
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   348
            } catch (CommunicationException ce) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   349
                communicationError = true;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   350
                throw ce;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   351
            } catch (NameNotFoundException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   352
                // name does not exist on this LDAP server
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   353
                // treat same as not attributes found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   354
                attrs = EMPTY_ATTRIBUTES;
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
            for (String attrId : requestedAttributes) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   357
                Attribute attr = attrs.get(attrId);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   358
                byte[][] values = getAttributeValues(attr);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   359
                cacheAttribute(attrId, values);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   360
                valueMap.put(attrId, values);
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
            return valueMap;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   363
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   364
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
         * Add the values to the cache.
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
        private void cacheAttribute(String attrId, byte[][] values) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   369
            String cacheKey = name + "|" + attrId;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   370
            valueCache.put(cacheKey, values);
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
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   373
        /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   374
         * 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
   375
         * 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
   376
         * 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
   377
         */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   378
        private byte[][] getAttributeValues(Attribute attr)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   379
                throws NamingException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   380
            byte[][] values;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   381
            if (attr == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   382
                values = BB0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   383
            } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   384
                values = new byte[attr.size()][];
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   385
                int i = 0;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   386
                NamingEnumeration<?> enum_ = attr.getAll();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   387
                while (enum_.hasMore()) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   388
                    Object obj = enum_.next();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   389
                    if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   390
                        if (obj instanceof String) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   391
                            debug.println("LDAPCertStore.getAttrValues() "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   392
                                + "enum.next is a string!: " + obj);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   393
                        }
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
                    byte[] value = (byte[])obj;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   396
                    values[i++] = value;
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
            return values;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   400
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   401
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
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   404
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   405
     * 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
   406
     * directory. Returns a Collection containing only the Certificates that
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   407
     * match the specified CertSelector.
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
     * @param name the location holding the attribute
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   410
     * @param id the attribute identifier
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   411
     * @param sel a CertSelector that the Certificates must match
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   412
     * @return a Collection of Certificates found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   413
     * @throws CertStoreException       if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   414
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   415
    private Collection<X509Certificate> getCertificates(LDAPRequest request,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   416
        String id, X509CertSelector sel) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   417
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   418
        /* fetch encoded certs from storage */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   419
        byte[][] encodedCert;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   420
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   421
            encodedCert = request.getValues(id);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   422
        } catch (NamingException namingEx) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   423
            throw new CertStoreException(namingEx);
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
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   426
        int n = encodedCert.length;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   427
        if (n == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   428
            return Collections.emptySet();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   429
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   430
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   431
        List<X509Certificate> certs = new ArrayList<>(n);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   432
        /* decode certs and check if they satisfy selector */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   433
        for (int i = 0; i < n; i++) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   434
            ByteArrayInputStream bais = new ByteArrayInputStream(encodedCert[i]);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   435
            try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   436
                Certificate cert = cf.generateCertificate(bais);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   437
                if (sel.match(cert)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   438
                  certs.add((X509Certificate)cert);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   439
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   440
            } catch (CertificateException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   441
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   442
                    debug.println("LDAPCertStore.getCertificates() encountered "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   443
                        + "exception while parsing cert, skipping the bad data: ");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   444
                    HexDumpEncoder encoder = new HexDumpEncoder();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   445
                    debug.println(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   446
                        "[ " + encoder.encodeBuffer(encodedCert[i]) + " ]");
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
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   450
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   451
        return certs;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   452
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   453
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   454
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   455
     * 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
   456
     * directory.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   457
     *
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   458
     * @param name the location holding the attribute
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   459
     * @param id the attribute identifier
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   460
     * @return a Collection of X509CertificatePairs found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   461
     * @throws CertStoreException       if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   462
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   463
    private Collection<X509CertificatePair> getCertPairs(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   464
        LDAPRequest request, String id) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   465
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   466
        /* fetch the encoded cert pairs from storage */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   467
        byte[][] encodedCertPair;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   468
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   469
            encodedCertPair = request.getValues(id);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   470
        } catch (NamingException namingEx) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   471
            throw new CertStoreException(namingEx);
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
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   474
        int n = encodedCertPair.length;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   475
        if (n == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   476
            return Collections.emptySet();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   477
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   478
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   479
        List<X509CertificatePair> certPairs = new ArrayList<>(n);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   480
        /* 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
   481
        for (int i = 0; i < n; i++) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   482
            try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   483
                X509CertificatePair certPair =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   484
                    X509CertificatePair.generateCertificatePair(encodedCertPair[i]);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   485
                certPairs.add(certPair);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   486
            } catch (CertificateException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   487
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   488
                    debug.println(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   489
                        "LDAPCertStore.getCertPairs() encountered exception "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   490
                        + "while parsing cert, skipping the bad data: ");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   491
                    HexDumpEncoder encoder = new HexDumpEncoder();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   492
                    debug.println(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   493
                        "[ " + encoder.encodeBuffer(encodedCertPair[i]) + " ]");
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
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   497
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   498
        return certPairs;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   499
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   500
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   501
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   502
     * Looks at certificate pairs stored in the crossCertificatePair attribute
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   503
     * 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
   504
     * 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
   505
     * 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
   506
     * component that match the reverse X509CertSelector.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   507
     * <p>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   508
     * 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
   509
     * corresponding component will be rejected.
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
     * @param name the location to look in
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   512
     * @param forward the forward X509CertSelector (or null)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   513
     * @param reverse the reverse X509CertSelector (or null)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   514
     * @return a Collection of X509Certificates found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   515
     * @throws CertStoreException       if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   516
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   517
    private Collection<X509Certificate> getMatchingCrossCerts(
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   518
            LDAPRequest request, X509CertSelector forward,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   519
            X509CertSelector reverse)
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   520
            throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   521
        // Get the cert pairs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   522
        Collection<X509CertificatePair> certPairs =
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   523
                                getCertPairs(request, CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   524
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   525
        // 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
   526
        ArrayList<X509Certificate> matchingCerts = new ArrayList<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   527
        for (X509CertificatePair certPair : certPairs) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   528
            X509Certificate cert;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   529
            if (forward != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   530
                cert = certPair.getForward();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   531
                if ((cert != null) && forward.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
            if (reverse != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   536
                cert = certPair.getReverse();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   537
                if ((cert != null) && reverse.match(cert)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   538
                    matchingCerts.add(cert);
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
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   541
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   542
        return matchingCerts;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   543
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   544
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   545
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   546
     * 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
   547
     * 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
   548
     * 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
   549
     * <p>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   550
     * 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
   551
     * matching <code>X509Certificate</code>s. Instead, the
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   552
     * <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
   553
     * 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
   554
     * to the PKIX LDAPv2 schema, RFC 2587).
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   555
     * 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
   556
     * 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
   557
     * 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
   558
     * <code>X509CertSelector</code>), a <code>CertStoreException</code> is
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   559
     * thrown.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   560
     *
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   561
     * @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
   562
     *  <code>Certificate</code>s should be returned.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   563
     * @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
   564
     *         match the specified selector
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   565
     * @throws CertStoreException if an exception occurs
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
    synchronized Collection<X509Certificate> getCertificates
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   568
        (X509CertSelector xsel, String ldapDN) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   569
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   570
        if (ldapDN == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   571
            ldapDN = xsel.getSubjectAsString();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   572
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   573
        int basicConstraints = xsel.getBasicConstraints();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   574
        String issuer = xsel.getIssuerAsString();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   575
        HashSet<X509Certificate> certs = new HashSet<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   576
        if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   577
            debug.println("LDAPCertStore.engineGetCertificates() basicConstraints: "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   578
                + basicConstraints);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   579
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   580
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   581
        // basicConstraints:
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   582
        // -2: only EE certs accepted
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   583
        // -1: no check is done
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   584
        //  0: any CA certificate accepted
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   585
        // >1: certificate's basicConstraints extension pathlen must match
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   586
        if (ldapDN != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   587
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   588
                debug.println("LDAPCertStore.engineGetCertificates() "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   589
                    + " subject is not null");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   590
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   591
            LDAPRequest request = new LDAPRequest(ldapDN);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   592
            if (basicConstraints > -2) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   593
                request.addRequestedAttribute(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   594
                request.addRequestedAttribute(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   595
                request.addRequestedAttribute(ARL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   596
                if (prefetchCRLs) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   597
                    request.addRequestedAttribute(CRL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   598
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   599
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   600
            if (basicConstraints < 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   601
                request.addRequestedAttribute(USER_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   602
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   603
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   604
            if (basicConstraints > -2) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   605
                certs.addAll(getMatchingCrossCerts(request, xsel, null));
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
                        + "getMatchingCrossCerts(subject,xsel,null),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
                certs.addAll(getCertificates(request, CA_CERT, xsel));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   612
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   613
                    debug.println("LDAPCertStore.engineGetCertificates() after "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   614
                        + "getCertificates(subject,CA_CERT,xsel),certs.size(): "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   615
                        + certs.size());
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   616
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   617
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   618
            if (basicConstraints < 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   619
                certs.addAll(getCertificates(request, USER_CERT, xsel));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   620
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   621
                    debug.println("LDAPCertStore.engineGetCertificates() after "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   622
                        + "getCertificates(subject,USER_CERT, xsel),certs.size(): "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   623
                        + certs.size());
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
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   626
        } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   627
            if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   628
                debug.println
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   629
                    ("LDAPCertStore.engineGetCertificates() subject is null");
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
            if (basicConstraints == -2) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   632
                throw new CertStoreException("need subject to find EE certs");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   633
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   634
            if (issuer == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   635
                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
   636
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   637
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   638
        if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   639
            debug.println("LDAPCertStore.engineGetCertificates() about to "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   640
                + "getMatchingCrossCerts...");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   641
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   642
        if ((issuer != null) && (basicConstraints > -2)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   643
            LDAPRequest request = new LDAPRequest(issuer);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   644
            request.addRequestedAttribute(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   645
            request.addRequestedAttribute(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   646
            request.addRequestedAttribute(ARL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   647
            if (prefetchCRLs) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   648
                request.addRequestedAttribute(CRL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   649
            }
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(getMatchingCrossCerts(request, null, 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
                    + "getMatchingCrossCerts(issuer,null,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
            certs.addAll(getCertificates(request, CA_CERT, xsel));
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() after "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   660
                    + "getCertificates(issuer,CA_CERT,xsel),certs.size(): "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   661
                    + certs.size());
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
        if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   665
            debug.println("LDAPCertStore.engineGetCertificates() returning certs");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   666
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   667
        return certs;
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
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   670
    /*
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   671
     * 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
   672
     * 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
   673
     * specified X509CRLSelector.
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
     * @param name the location holding the attribute
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   676
     * @param id the attribute identifier
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   677
     * @param sel a X509CRLSelector that the CRLs must match
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   678
     * @return a Collection of CRLs found
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   679
     * @throws CertStoreException       if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   680
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   681
    private Collection<X509CRL> getCRLs(LDAPRequest request, String id,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   682
            X509CRLSelector sel) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   683
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   684
        /* fetch the encoded crls from storage */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   685
        byte[][] encodedCRL;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   686
        try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   687
            encodedCRL = request.getValues(id);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   688
        } catch (NamingException namingEx) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   689
            throw new CertStoreException(namingEx);
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
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   692
        int n = encodedCRL.length;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   693
        if (n == 0) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   694
            return Collections.emptySet();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   695
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   696
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   697
        List<X509CRL> crls = new ArrayList<>(n);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   698
        /* decode each crl and check if it matches selector */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   699
        for (int i = 0; i < n; i++) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   700
            try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   701
                CRL crl = cf.generateCRL(new ByteArrayInputStream(encodedCRL[i]));
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   702
                if (sel.match(crl)) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   703
                    crls.add((X509CRL)crl);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   704
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   705
            } catch (CRLException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   706
                if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   707
                    debug.println("LDAPCertStore.getCRLs() encountered exception"
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   708
                        + " while parsing CRL, skipping the bad data: ");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   709
                    HexDumpEncoder encoder = new HexDumpEncoder();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   710
                    debug.println("[ " + encoder.encodeBuffer(encodedCRL[i]) + " ]");
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
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   714
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   715
        return crls;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   716
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   717
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   718
    /**
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   719
     * 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
   720
     * 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
   721
     * 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
   722
     * <p>
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   723
     * 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
   724
     * 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
   725
     * 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
   726
     * 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
   727
     * 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
   728
     * 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
   729
     * (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
   730
     * <code>CertStoreException</code> is thrown.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   731
     *
48582
02176e56d91c 8186606: Improve LDAP lookup robustness
weijun
parents: 47216
diff changeset
   732
     * @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
   733
     *  <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
   734
     *  to return all <code>CRL</code>s.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   735
     * @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
   736
     *         match the specified selector
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   737
     * @throws CertStoreException if an exception occurs
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   738
     */
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   739
    synchronized Collection<X509CRL> getCRLs(X509CRLSelector xsel,
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   740
         String ldapDN) throws CertStoreException {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   741
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   742
        HashSet<X509CRL> crls = new HashSet<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   743
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   744
        // 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
   745
        Collection<Object> issuerNames;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   746
        X509Certificate certChecking = xsel.getCertificateChecking();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   747
        if (certChecking != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   748
            issuerNames = new HashSet<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   749
            X500Principal issuer = certChecking.getIssuerX500Principal();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   750
            issuerNames.add(issuer.getName(X500Principal.RFC2253));
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
            // 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
   753
            // entries of all acceptable CRL issuers
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   754
            if (ldapDN != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   755
                issuerNames = new HashSet<>();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   756
                issuerNames.add(ldapDN);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   757
            } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   758
                issuerNames = xsel.getIssuerNames();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   759
                if (issuerNames == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   760
                    throw new CertStoreException("need issuerNames or"
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   761
                       + " certChecking to find CRLs");
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   762
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   763
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   764
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   765
        for (Object nameObject : issuerNames) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   766
            String issuerName;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   767
            if (nameObject instanceof byte[]) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   768
                try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   769
                    X500Principal issuer = new X500Principal((byte[])nameObject);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   770
                    issuerName = issuer.getName(X500Principal.RFC2253);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   771
                } catch (IllegalArgumentException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   772
                    continue;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   773
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   774
            } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   775
                issuerName = (String)nameObject;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   776
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   777
            // 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
   778
            Collection<X509CRL> entryCRLs = Collections.emptySet();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   779
            if (certChecking == null || certChecking.getBasicConstraints() != -1) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   780
                LDAPRequest request = new LDAPRequest(issuerName);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   781
                request.addRequestedAttribute(CROSS_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   782
                request.addRequestedAttribute(CA_CERT);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   783
                request.addRequestedAttribute(ARL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   784
                if (prefetchCRLs) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   785
                    request.addRequestedAttribute(CRL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   786
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   787
                try {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   788
                    entryCRLs = getCRLs(request, ARL, xsel);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   789
                    if (entryCRLs.isEmpty()) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   790
                        // 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
   791
                        // 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
   792
                        prefetchCRLs = true;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   793
                    } else {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   794
                        crls.addAll(entryCRLs);
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
                } catch (CertStoreException e) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   797
                    if (debug != null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   798
                        debug.println("LDAPCertStore.engineGetCRLs non-fatal error "
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   799
                            + "retrieving ARLs:" + e);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   800
                        e.printStackTrace();
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   801
                    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   802
                }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   803
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   804
            // Otherwise, get the CRL
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   805
            // 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
   806
            // attribute, so check both for matching CRLs.
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   807
            if (entryCRLs.isEmpty() || certChecking == null) {
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   808
                LDAPRequest request = new LDAPRequest(issuerName);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   809
                request.addRequestedAttribute(CRL);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   810
                entryCRLs = getCRLs(request, CRL, xsel);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   811
                crls.addAll(entryCRLs);
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   812
            }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   813
        }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   814
        return crls;
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   815
    }
1998a5644f50 8038084: CertStore needs a way to add new CertStore types
valeriep
parents:
diff changeset
   816
}