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