Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/x509/InhibitAnyPolicyExtension.java
41159 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.x509;
27
28
import java.io.IOException;
29
import java.io.OutputStream;
30
import java.util.Enumeration;
31
32
import sun.security.util.*;
33
34
/**
35
* This class represents the Inhibit Any-Policy Extension.
36
*
37
* <p>The inhibit any-policy extension can be used in certificates issued
38
* to CAs. The inhibit any-policy indicates that the special any-policy
39
* OID, with the value {2 5 29 32 0}, is not considered an explicit
40
* match for other certificate policies. The value indicates the number
41
* of additional certificates that may appear in the path before any-
42
* policy is no longer permitted. For example, a value of one indicates
43
* that any-policy may be processed in certificates issued by the sub-
44
* ject of this certificate, but not in additional certificates in the
45
* path.
46
* <p>
47
* This extension MUST be critical.
48
* <p>
49
* The ASN.1 syntax for this extension is:
50
* <pre>{@code
51
* id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 }
52
*
53
* InhibitAnyPolicy ::= SkipCerts
54
*
55
* SkipCerts ::= INTEGER (0..MAX)
56
* }</pre>
57
* @author Anne Anderson
58
* @see CertAttrSet
59
* @see Extension
60
*/
61
public class InhibitAnyPolicyExtension extends Extension
62
implements CertAttrSet<String> {
63
64
private static final Debug debug = Debug.getInstance("certpath");
65
66
/**
67
* Identifier for this attribute, to be used with the
68
* get, set, delete methods of Certificate, x509 type.
69
*/
70
public static final String IDENT = "x509.info.extensions.InhibitAnyPolicy";
71
72
/**
73
* Object identifier for "any-policy"
74
*/
75
public static ObjectIdentifier AnyPolicy_Id =
76
ObjectIdentifier.of(KnownOIDs.CE_CERT_POLICIES_ANY);
77
78
/**
79
* Attribute names.
80
*/
81
public static final String NAME = "InhibitAnyPolicy";
82
public static final String SKIP_CERTS = "skip_certs";
83
84
// Private data members
85
private int skipCerts = Integer.MAX_VALUE;
86
87
// Encode this extension value
88
private void encodeThis() throws IOException {
89
DerOutputStream out = new DerOutputStream();
90
out.putInteger(skipCerts);
91
this.extensionValue = out.toByteArray();
92
}
93
94
/**
95
* Default constructor for this object.
96
*
97
* @param skipCerts specifies the depth of the certification path.
98
* Use value of -1 to request unlimited depth.
99
*/
100
public InhibitAnyPolicyExtension(int skipCerts) throws IOException {
101
if (skipCerts < -1)
102
throw new IOException("Invalid value for skipCerts");
103
if (skipCerts == -1)
104
this.skipCerts = Integer.MAX_VALUE;
105
else
106
this.skipCerts = skipCerts;
107
this.extensionId = PKIXExtensions.InhibitAnyPolicy_Id;
108
critical = true;
109
encodeThis();
110
}
111
112
/**
113
* Create the extension from the passed DER encoded value of the same.
114
*
115
* @param critical criticality flag to use. Must be true for this
116
* extension.
117
* @param value a byte array holding the DER-encoded extension value.
118
* @exception ClassCastException if value is not an array of bytes
119
* @exception IOException on error.
120
*/
121
public InhibitAnyPolicyExtension(Boolean critical, Object value)
122
throws IOException {
123
124
this.extensionId = PKIXExtensions.InhibitAnyPolicy_Id;
125
126
if (!critical.booleanValue())
127
throw new IOException("Criticality cannot be false for " +
128
"InhibitAnyPolicy");
129
this.critical = critical.booleanValue();
130
131
this.extensionValue = (byte[]) value;
132
DerValue val = new DerValue(this.extensionValue);
133
if (val.tag != DerValue.tag_Integer)
134
throw new IOException("Invalid encoding of InhibitAnyPolicy: "
135
+ "data not integer");
136
137
if (val.data == null)
138
throw new IOException("Invalid encoding of InhibitAnyPolicy: "
139
+ "null data");
140
int skipCertsValue = val.getInteger();
141
if (skipCertsValue < -1)
142
throw new IOException("Invalid value for skipCerts");
143
if (skipCertsValue == -1) {
144
this.skipCerts = Integer.MAX_VALUE;
145
} else {
146
this.skipCerts = skipCertsValue;
147
}
148
}
149
150
/**
151
* Return user readable form of extension.
152
*/
153
public String toString() {
154
String s = super.toString() + "InhibitAnyPolicy: " + skipCerts + "\n";
155
return s;
156
}
157
158
/**
159
* Encode this extension value to the output stream.
160
*
161
* @param out the DerOutputStream to encode the extension to.
162
*/
163
public void encode(OutputStream out) throws IOException {
164
DerOutputStream tmp = new DerOutputStream();
165
if (extensionValue == null) {
166
this.extensionId = PKIXExtensions.InhibitAnyPolicy_Id;
167
critical = true;
168
encodeThis();
169
}
170
super.encode(tmp);
171
172
out.write(tmp.toByteArray());
173
}
174
175
/**
176
* Set the attribute value.
177
*
178
* @param name name of attribute to set. Must be SKIP_CERTS.
179
* @param obj value to which attribute is to be set. Must be Integer
180
* type.
181
* @throws IOException on error
182
*/
183
public void set(String name, Object obj) throws IOException {
184
if (name.equalsIgnoreCase(SKIP_CERTS)) {
185
if (!(obj instanceof Integer))
186
throw new IOException("Attribute value should be of type Integer.");
187
int skipCertsValue = ((Integer)obj).intValue();
188
if (skipCertsValue < -1)
189
throw new IOException("Invalid value for skipCerts");
190
if (skipCertsValue == -1) {
191
skipCerts = Integer.MAX_VALUE;
192
} else {
193
skipCerts = skipCertsValue;
194
}
195
} else
196
throw new IOException("Attribute name not recognized by " +
197
"CertAttrSet:InhibitAnyPolicy.");
198
encodeThis();
199
}
200
201
/**
202
* Get the attribute value.
203
*
204
* @param name name of attribute to get. Must be SKIP_CERTS.
205
* @return value of the attribute. In this case it will be of type
206
* Integer.
207
* @throws IOException on error
208
*/
209
public Integer get(String name) throws IOException {
210
if (name.equalsIgnoreCase(SKIP_CERTS))
211
return (skipCerts);
212
else
213
throw new IOException("Attribute name not recognized by " +
214
"CertAttrSet:InhibitAnyPolicy.");
215
}
216
217
/**
218
* Delete the attribute value.
219
*
220
* @param name name of attribute to delete. Must be SKIP_CERTS.
221
* @throws IOException on error. In this case, IOException will always be
222
* thrown, because the only attribute, SKIP_CERTS, is
223
* required.
224
*/
225
public void delete(String name) throws IOException {
226
if (name.equalsIgnoreCase(SKIP_CERTS))
227
throw new IOException("Attribute " + SKIP_CERTS +
228
" may not be deleted.");
229
else
230
throw new IOException("Attribute name not recognized by " +
231
"CertAttrSet:InhibitAnyPolicy.");
232
}
233
234
/**
235
* Return an enumeration of names of attributes existing within this
236
* attribute.
237
*
238
* @return enumeration of elements
239
*/
240
public Enumeration<String> getElements() {
241
AttributeNameEnumeration elements = new AttributeNameEnumeration();
242
elements.addElement(SKIP_CERTS);
243
return (elements.elements());
244
}
245
246
/**
247
* Return the name of this attribute.
248
*
249
* @return name of attribute.
250
*/
251
public String getName() {
252
return (NAME);
253
}
254
}
255
256