jdk/src/java.base/share/classes/sun/security/provider/certpath/PolicyNodeImpl.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Implements the <code>PolicyNode</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class provides an implementation of the <code>PolicyNode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * interface, and is used internally to build and search Policy Trees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * While the implementation is mutable during construction, it is immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * before returning to a client and no mutable public or protected methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * are exposed by this implementation, as per the contract of PolicyNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author      Seth Proctor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class PolicyNodeImpl implements PolicyNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Use to specify the special policy "Any Policy"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final String ANY_POLICY = "2.5.29.32.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // every node has one parent, and zero or more children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private PolicyNodeImpl mParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private HashSet<PolicyNodeImpl> mChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    59
    // the 4 fields specified by RFC 5280
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private String mValidPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private HashSet<PolicyQualifierInfo> mQualifierSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean mCriticalityIndicator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private HashSet<String> mExpectedPolicySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean mOriginalExpectedPolicySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // the tree depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int mDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // immutability flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private boolean isImmutable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Constructor which takes a <code>PolicyNodeImpl</code> representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * parent in the Policy Tree to this node. If null, this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * root of the tree. The constructor also takes the associated data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * for this node, as found in the certificate. It also takes a boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * argument specifying whether this node is being created as a result
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * of policy mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param parent the PolicyNode above this in the tree, or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *               node is the tree's root node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param validPolicy a String representing this node's valid policy OID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param qualifierSet the Set of qualifiers for this policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param criticalityIndicator a boolean representing whether or not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *                             extension is critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param expectedPolicySet a Set of expected policies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param generatedByPolicyMapping a boolean indicating whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * node was generated by a policy mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    PolicyNodeImpl(PolicyNodeImpl parent, String validPolicy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                Set<PolicyQualifierInfo> qualifierSet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                boolean criticalityIndicator, Set<String> expectedPolicySet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                boolean generatedByPolicyMapping) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        mParent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        mChildren = new HashSet<PolicyNodeImpl>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (validPolicy != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            mValidPolicy = validPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            mValidPolicy = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (qualifierSet != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            mQualifierSet = new HashSet<PolicyQualifierInfo>(qualifierSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            mQualifierSet = new HashSet<PolicyQualifierInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        mCriticalityIndicator = criticalityIndicator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (expectedPolicySet != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            mExpectedPolicySet = new HashSet<String>(expectedPolicySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            mExpectedPolicySet = new HashSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        mOriginalExpectedPolicySet = !generatedByPolicyMapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // see if we're the root, and act appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (mParent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            mDepth = mParent.getDepth() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            mParent.addChild(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            mDepth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Alternate constructor which makes a new node with the policy data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * in an existing <code>PolicyNodeImpl</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param parent a PolicyNode that's the new parent of the node, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *               null if this is the root node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param node a PolicyNode containing the policy data to copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    PolicyNodeImpl(PolicyNodeImpl parent, PolicyNodeImpl node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this(parent, node.mValidPolicy, node.mQualifierSet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
             node.mCriticalityIndicator, node.mExpectedPolicySet, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   137
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public PolicyNode getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return mParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   142
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public Iterator<PolicyNodeImpl> getChildren() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return Collections.unmodifiableSet(mChildren).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   147
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public int getDepth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return mDepth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   152
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public String getValidPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return mValidPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   157
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public Set<PolicyQualifierInfo> getPolicyQualifiers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return Collections.unmodifiableSet(mQualifierSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   162
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public Set<String> getExpectedPolicies() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return Collections.unmodifiableSet(mExpectedPolicySet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   167
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public boolean isCritical() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return mCriticalityIndicator;
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
     * Return a printable representation of the PolicyNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Starting at the node on which this method is called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * it recurses through the tree and prints out each node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return a String describing the contents of the Policy Node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   179
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public String toString() {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   181
        StringBuilder buffer = new StringBuilder(this.asString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   183
        for (PolicyNodeImpl node : mChildren) {
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   184
            buffer.append(node);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // private methods and package private operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    boolean isImmutable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return isImmutable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Sets the immutability flag of this node and all of its children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    void setImmutable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (isImmutable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        for (PolicyNodeImpl node : mChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            node.setImmutable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        isImmutable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Private method sets a child node. This is called from the child's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param child new <code>PolicyNodeImpl</code> child node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private void addChild(PolicyNodeImpl child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (isImmutable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new IllegalStateException("PolicyNode is immutable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        mChildren.add(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Adds an expectedPolicy to the expected policy set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * If this is the original expected policy set initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * by the constructor, then the expected policy set is cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * before the expected policy is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param expectedPolicy a String representing an expected policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    void addExpectedPolicy(String expectedPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (isImmutable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new IllegalStateException("PolicyNode is immutable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (mOriginalExpectedPolicySet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            mExpectedPolicySet.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            mOriginalExpectedPolicySet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        mExpectedPolicySet.add(expectedPolicy);
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
     * Removes all paths which don't reach the specified depth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param depth an int representing the desired minimum depth of all paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    void prune(int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (isImmutable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new IllegalStateException("PolicyNode is immutable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // if we have no children, we can't prune below us...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (mChildren.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Iterator<PolicyNodeImpl> it = mChildren.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            PolicyNodeImpl node = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            node.prune(depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // now that we've called prune on the child, see if we should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // remove it from the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if ((node.mChildren.size() == 0) && (depth > mDepth + 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Deletes the specified child node of this node, if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param childNode the child node to be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    void deleteChild(PolicyNode childNode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (isImmutable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw new IllegalStateException("PolicyNode is immutable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        mChildren.remove(childNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns a copy of the tree, without copying the policy-related data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * rooted at the node on which this was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return a copy of the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    PolicyNodeImpl copyTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return copyTree(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private PolicyNodeImpl copyTree(PolicyNodeImpl parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        PolicyNodeImpl newNode = new PolicyNodeImpl(parent, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        for (PolicyNodeImpl node : mChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            node.copyTree(newNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return newNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Returns all nodes at the specified depth in the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param depth an int representing the depth of the desired nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @return a <code>Set</code> of all nodes at the specified depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    Set<PolicyNodeImpl> getPolicyNodes(int depth) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   303
        Set<PolicyNodeImpl> set = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        getPolicyNodes(depth, set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Add all nodes at depth depth to set and return the Set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Internal recursion helper.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private void getPolicyNodes(int depth, Set<PolicyNodeImpl> set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // if we've reached the desired depth, then return ourself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (mDepth == depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            set.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            for (PolicyNodeImpl node : mChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                node.getPolicyNodes(depth, set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Finds all nodes at the specified depth whose expected_policy_set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * contains the specified expected OID (if matchAny is false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * or the special OID "any value" (if matchAny is true).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param depth an int representing the desired depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param expectedOID a String encoding the valid OID to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param matchAny a boolean indicating whether an expected_policy_set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * containing ANY_POLICY should be considered a match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return a Set of matched <code>PolicyNode</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    Set<PolicyNodeImpl> getPolicyNodesExpected(int depth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        String expectedOID, boolean matchAny) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (expectedOID.equals(ANY_POLICY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return getPolicyNodes(depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return getPolicyNodesExpectedHelper(depth, expectedOID, matchAny);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private Set<PolicyNodeImpl> getPolicyNodesExpectedHelper(int depth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        String expectedOID, boolean matchAny) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   347
        HashSet<PolicyNodeImpl> set = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (mDepth < depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            for (PolicyNodeImpl node : mChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                set.addAll(node.getPolicyNodesExpectedHelper(depth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                                             expectedOID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                                             matchAny));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (matchAny) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                if (mExpectedPolicySet.contains(ANY_POLICY))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    set.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (mExpectedPolicySet.contains(expectedOID))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    set.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Finds all nodes at the specified depth that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * specified valid OID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param depth an int representing the desired depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param validOID a String encoding the valid OID to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @return a Set of matched <code>PolicyNode</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    Set<PolicyNodeImpl> getPolicyNodesValid(int depth, String validOID) {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   377
        HashSet<PolicyNodeImpl> set = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (mDepth < depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            for (PolicyNodeImpl node : mChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                set.addAll(node.getPolicyNodesValid(depth, validOID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (mValidPolicy.equals(validOID))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                set.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private static String policyToString(String oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (oid.equals(ANY_POLICY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return "anyPolicy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Prints out some data on this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    String asString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (mParent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return "anyPolicy  ROOT\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        } else {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 5506
diff changeset
   406
            StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            for (int i = 0, n = getDepth(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                sb.append("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            sb.append(policyToString(getValidPolicy()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            sb.append("  CRIT: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            sb.append(isCritical());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            sb.append("  EP: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            for (String policy : getExpectedPolicies()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                sb.append(policyToString(policy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                sb.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            sb.append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            sb.append(getDepth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            sb.append(")\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
}