jdk/src/java.base/share/classes/sun/security/provider/certpath/Builder.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 29973 188affdeeed2
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3841
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    29
import java.security.AccessController;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    34
import sun.security.action.GetBooleanAction;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    35
import sun.security.provider.certpath.PKIX.BuilderParams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.x509.GeneralNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.x509.GeneralNameInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.x509.GeneralSubtrees;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.x509.NameConstraintsExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.x509.SubjectAlternativeNameExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Abstract class representing a builder, which is able to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * matching certificates and is able to verify a particular certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author      Yassir Elley
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public abstract class Builder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final Debug debug = Debug.getInstance("certpath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private Set<String> matchingPolicies;
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    58
    final BuilderParams buildParams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    final X509CertSelector targetCertConstraints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Flag indicating whether support for the caIssuers field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Authority Information Access extension shall be enabled. Currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * disabled by default for compatibility reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29973
diff changeset
    66
    static final boolean USE_AIA = AccessController.doPrivileged
3841
6738c111d48f 6745437: Add option to only check revocation of end-entity certificate in a chain of certificates
mullan
parents: 2
diff changeset
    67
        (new GetBooleanAction("com.sun.security.enableAIAcaIssuers"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Initialize the builder with the input parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param params the parameter set used to build a certification path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    74
    Builder(BuilderParams buildParams) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.buildParams = buildParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.targetCertConstraints =
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
    77
            (X509CertSelector)buildParams.targetCertConstraints();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Retrieves certificates from the list of certStores using the buildParams
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * and the currentState as a filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param currentState the current State
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param certStores list of CertStores
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    abstract Collection<X509Certificate> getMatchingCerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        (State currentState, List<CertStore> certStores)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throws CertStoreException, CertificateException, IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Verifies the cert against the currentState, using the certPathList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * generated thus far to help with loop detection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param cert the certificate to be verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param currentState the current state against which the cert is verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param certPathList the certPathList generated thus far
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    abstract void verifyCert(X509Certificate cert, State currentState,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   100
                             List<X509Certificate> certPathList)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   101
        throws GeneralSecurityException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Verifies whether the input certificate completes the path.
29973
188affdeeed2 7194452: Remove "Reverse" PKIX CertPathBuilder implementation
juh
parents: 29264
diff changeset
   105
     * When building in the forward direction, a trust anchor will
188affdeeed2 7194452: Remove "Reverse" PKIX CertPathBuilder implementation
juh
parents: 29264
diff changeset
   106
     * complete the path.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param cert the certificate to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @return a boolean value indicating whether the cert completes the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    abstract boolean isPathCompleted(X509Certificate cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Adds the certificate to the certPathList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param cert the certificate to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param certPathList the certification path list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    abstract void addCertToPath(X509Certificate cert,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   120
                                LinkedList<X509Certificate> certPathList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Removes final certificate from the certPathList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param certPathList the certification path list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    abstract void removeFinalCertFromPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        (LinkedList<X509Certificate> certPathList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * get distance of one GeneralName from another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param base GeneralName at base of subtree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param test GeneralName to be tested against base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param incomparable the value to return if the names are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *  incomparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return distance of test name from base, where 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *         means exact match, 1 means test is an immediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *         child of base, 2 means test is a grandchild, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *         -1 means test is a parent of base, -2 means test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         is a grandparent, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static int distance(GeneralNameInterface base,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   144
                        GeneralNameInterface test, int incomparable)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   145
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        switch (base.constrains(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        case GeneralNameInterface.NAME_DIFF_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                debug.println("Builder.distance(): Names are different types");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   151
            return incomparable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        case GeneralNameInterface.NAME_SAME_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                debug.println("Builder.distance(): Names are same type but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    "in different subtrees");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return incomparable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        case GeneralNameInterface.NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        case GeneralNameInterface.NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        case GeneralNameInterface.NAME_NARROWS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        default: // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return incomparable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /* names are in same subtree */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return test.subtreeDepth() - base.subtreeDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * get hop distance of one GeneralName from another in links where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * the names need not have an ancestor/descendant relationship.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * For example, the hop distance from ou=D,ou=C,o=B,c=US to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * ou=F,ou=E,ou=C,o=B,c=US is 3: D->C, C->E, E->F.  The hop distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * from ou=C,o=B,c=US to ou=D,ou=C,o=B,c=US is -1: C->D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param base GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param test GeneralName to be tested against base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param incomparable the value to return if the names are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *  incomparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return distance of test name from base measured in hops in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         namespace hierarchy, where 0 means exact match.  Result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *         is positive if path is some number of up hops followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *         some number of down hops; result is negative if path is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         some number of down hops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    static int hops(GeneralNameInterface base, GeneralNameInterface test,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   190
                    int incomparable)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   191
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int baseRtest = base.constrains(test);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        switch (baseRtest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        case GeneralNameInterface.NAME_DIFF_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                debug.println("Builder.hops(): Names are different types");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return incomparable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        case GeneralNameInterface.NAME_SAME_TYPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            /* base and test are in different subtrees */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        case GeneralNameInterface.NAME_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            /* base matches test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        case GeneralNameInterface.NAME_WIDENS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            /* base is ancestor of test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return (test.subtreeDepth()-base.subtreeDepth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        case GeneralNameInterface.NAME_NARROWS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            /* base is descendant of test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return (test.subtreeDepth()-base.subtreeDepth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        default: // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return incomparable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        /* names are in different subtrees */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (base.getType() != GeneralNameInterface.NAME_DIRECTORY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                debug.println("Builder.hops(): hopDistance not implemented " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    "for this name type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return incomparable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        X500Name baseName = (X500Name)base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        X500Name testName = (X500Name)test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        X500Name commonName = baseName.commonAncestor(testName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (commonName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                debug.println("Builder.hops(): Names are in different " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    "namespaces");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return incomparable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            int commonDistance = commonName.subtreeDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            int baseDistance = baseName.subtreeDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            int testDistance = testName.subtreeDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return (baseDistance + testDistance - (2 * commonDistance));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Determine how close a given certificate gets you toward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * a given target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param constraints Current NameConstraints; if null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *        then caller must verify NameConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *        independently, realizing that this certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *        may not actually lead to the target at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param cert Candidate certificate for chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param target GeneralNameInterface name of target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return distance from this certificate to target:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <li>-1 means certificate could be CA for target, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *     there are no NameConstraints limiting how close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <li> 0 means certificate subject or subjectAltName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *      matches target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <li> 1 means certificate is permitted to be CA for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *      target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <li> 2 means certificate is permitted to be CA for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *      parent of target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <li>&gt;0 in general, means certificate is permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *     to be a CA for this distance higher in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *     hierarchy than the target, plus 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <p>Note that the subject and/or subjectAltName of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * candidate cert does not have to be an ancestor of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * target in order to be a CA that can issue a certificate to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * the target. In these cases, the target distance is calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * by inspecting the NameConstraints extension in the candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * certificate. For example, suppose the target is an X.500 DN with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * a value of "CN=mullan,OU=ireland,O=sun,C=us" and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * NameConstraints extension in the candidate certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * includes a permitted component of "O=sun,C=us", which implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * that the candidate certificate is allowed to issue certs in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * the "O=sun,C=us" namespace. The target distance is 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * ((distance of permitted NC from target) + 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * The (+1) is added to distinguish the result from the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * which returns (0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @throws IOException if certificate does not get closer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    static int targetDistance(NameConstraintsExtension constraints,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   281
                              X509Certificate cert, GeneralNameInterface target)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   282
            throws IOException
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   283
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        /* ensure that certificate satisfies existing name constraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (constraints != null && !constraints.verify(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            throw new IOException("certificate does not satisfy existing name "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                + "constraints");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        X509CertImpl certImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            certImpl = X509CertImpl.toImpl(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } catch (CertificateException e) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   294
            throw new IOException("Invalid certificate", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        /* see if certificate subject matches target */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        X500Name subject = X500Name.asX500Name(certImpl.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (subject.equals(target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            /* match! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        SubjectAlternativeNameExtension altNameExt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            certImpl.getSubjectAlternativeNameExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (altNameExt != null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   306
            GeneralNames altNames = altNameExt.get(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   307
                    SubjectAlternativeNameExtension.SUBJECT_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            /* see if any alternative name matches target */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (altNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                for (int j = 0, n = altNames.size(); j < n; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    GeneralNameInterface altName = altNames.get(j).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    if (altName.equals(target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /* no exact match; see if certificate can get us to target */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        /* first, get NameConstraints out of certificate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        NameConstraintsExtension ncExt = certImpl.getNameConstraintsExtension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (ncExt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        /* merge certificate's NameConstraints with current NameConstraints */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (constraints != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            constraints.merge(ncExt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // Make sure we do a clone here, because we're probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // going to modify this object later and we don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // be sharing it with a Certificate object!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            constraints = (NameConstraintsExtension) ncExt.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            debug.println("Builder.targetDistance() merged constraints: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                + String.valueOf(constraints));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /* reduce permitted by excluded */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   343
        GeneralSubtrees permitted =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   344
                constraints.get(NameConstraintsExtension.PERMITTED_SUBTREES);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   345
        GeneralSubtrees excluded =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   346
                constraints.get(NameConstraintsExtension.EXCLUDED_SUBTREES);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (permitted != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            permitted.reduce(excluded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            debug.println("Builder.targetDistance() reduced constraints: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                + permitted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        /* see if new merged constraints allow target */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (!constraints.verify(target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new IOException("New certificate not allowed to sign "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                + "certificate for target");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        /* find distance to target, if any, in permitted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (permitted == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            /* certificate is unconstrained; could sign for anything */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        for (int i = 0, n = permitted.size(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            GeneralNameInterface perName = permitted.get(i).getName().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            int distance = distance(perName, target, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (distance >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                return (distance + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        /* no matching type in permitted; cert holder could certify target */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * This method can be used as an optimization to filter out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * certificates that do not have policies which are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * It returns the set of policies (String OIDs) that should exist in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * the certificate policies extension of the certificate that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * needed by the builder. The logic applied is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *   1) If some initial policies have been set *and* policy mappings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *   inhibited, then acceptable certificates are those that include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *   the ANY_POLICY OID or with policies that intersect with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *   initial policies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *   2) If no initial policies have been set *or* policy mappings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *   not inhibited then we don't have much to work with. All we know is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *   that a certificate must have *some* policy because if it didn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *   have any policy then the policy tree would become null (and validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *   would fail).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return the Set of policies any of which must exist in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * cert's certificate policies extension in order for a cert to be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    Set<String> getMatchingPolicies() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (matchingPolicies != null) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   397
            Set<String> initialPolicies = buildParams.initialPolicies();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if ((!initialPolicies.isEmpty()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                (!initialPolicies.contains(PolicyChecker.ANY_POLICY)) &&
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   400
                (buildParams.policyMappingInhibited()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   402
                matchingPolicies = new HashSet<>(initialPolicies);
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   403
                matchingPolicies.add(PolicyChecker.ANY_POLICY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                // we just return an empty set to make sure that there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                // at least a certificate policies extension in the cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                matchingPolicies = Collections.<String>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return matchingPolicies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Search the specified CertStores and add all certificates matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * selector to resultCerts. Self-signed certs are not useful here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * and therefore ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * If the targetCert criterion of the selector is set, only that cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * is examined and the CertStores are not searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * If checkAll is true, all CertStores are searched for matching certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * If false, the method returns as soon as the first CertStore returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * a matching cert(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Returns true iff resultCerts changed (a cert was added to the collection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    boolean addMatchingCerts(X509CertSelector selector,
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   428
                             Collection<CertStore> certStores,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   429
                             Collection<X509Certificate> resultCerts,
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   430
                             boolean checkAll)
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   431
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        X509Certificate targetCert = selector.getCertificate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (targetCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // no need to search CertStores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (selector.match(targetCert) && !X509CertImpl.isSelfSigned
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   436
                (targetCert, buildParams.sigProvider())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (debug != null) {
29264
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   438
                    debug.println("Builder.addMatchingCerts: " +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   439
                        "adding target cert" +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   440
                        "\n  SN: " + Debug.toHexString(
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   441
                                            targetCert.getSerialNumber()) +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   442
                        "\n  Subject: " + targetCert.getSubjectX500Principal() +
5172066a2da6 8054037: Improve tracing for java.security.debug=certpath
juh
parents: 25859
diff changeset
   443
                        "\n  Issuer: " + targetCert.getIssuerX500Principal());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                return resultCerts.add(targetCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        boolean add = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        for (CertStore store : certStores) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                Collection<? extends Certificate> certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                        store.getCertificates(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                for (Certificate cert : certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    if (!X509CertImpl.isSelfSigned
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10336
diff changeset
   456
                        ((X509Certificate)cert, buildParams.sigProvider())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        if (resultCerts.add((X509Certificate)cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            add = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                if (!checkAll && add) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            } catch (CertStoreException cse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                // if getCertificates throws a CertStoreException, we ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                // it and move on to the next CertStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    debug.println("Builder.addMatchingCerts, non-fatal " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        "exception retrieving certs: " + cse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    cse.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return add;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
}