author | igerasim |
Tue, 02 Oct 2018 10:19:07 -0700 | |
changeset 51986 | c1db377f6300 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
28420
8fe17d073b54
8059916: Change default criticality of policy mappings and policy constraints certificate extensions
juh
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 defines the certificate extension which specifies the |
|
36 |
* Policy constraints. |
|
37 |
* <p> |
|
38 |
* The policy constraints extension can be used in certificates issued |
|
39 |
* to CAs. The policy constraints extension constrains path validation |
|
40 |
* in two ways. It can be used to prohibit policy mapping or require |
|
41 |
* that each certificate in a path contain an acceptable policy |
|
42 |
* identifier.<p> |
|
43 |
* The ASN.1 syntax for this is (IMPLICIT tagging is defined in the |
|
44 |
* module definition): |
|
45 |
* <pre> |
|
46 |
* PolicyConstraints ::= SEQUENCE { |
|
47 |
* requireExplicitPolicy [0] SkipCerts OPTIONAL, |
|
48 |
* inhibitPolicyMapping [1] SkipCerts OPTIONAL |
|
49 |
* } |
|
50 |
* SkipCerts ::= INTEGER (0..MAX) |
|
51 |
* </pre> |
|
52 |
* @author Amit Kapoor |
|
53 |
* @author Hemma Prafullchandra |
|
54 |
* @see Extension |
|
55 |
* @see CertAttrSet |
|
56 |
*/ |
|
57 |
public class PolicyConstraintsExtension extends Extension |
|
58 |
implements CertAttrSet<String> { |
|
59 |
/** |
|
60 |
* Identifier for this attribute, to be used with the |
|
61 |
* get, set, delete methods of Certificate, x509 type. |
|
62 |
*/ |
|
63 |
public static final String IDENT = "x509.info.extensions.PolicyConstraints"; |
|
64 |
/** |
|
65 |
* Attribute names. |
|
66 |
*/ |
|
67 |
public static final String NAME = "PolicyConstraints"; |
|
68 |
public static final String REQUIRE = "require"; |
|
69 |
public static final String INHIBIT = "inhibit"; |
|
70 |
||
71 |
private static final byte TAG_REQUIRE = 0; |
|
72 |
private static final byte TAG_INHIBIT = 1; |
|
73 |
||
74 |
private int require = -1; |
|
75 |
private int inhibit = -1; |
|
76 |
||
77 |
// Encode this extension value. |
|
78 |
private void encodeThis() throws IOException { |
|
79 |
if (require == -1 && inhibit == -1) { |
|
80 |
this.extensionValue = null; |
|
81 |
return; |
|
82 |
} |
|
83 |
DerOutputStream tagged = new DerOutputStream(); |
|
84 |
DerOutputStream seq = new DerOutputStream(); |
|
85 |
||
86 |
if (require != -1) { |
|
87 |
DerOutputStream tmp = new DerOutputStream(); |
|
88 |
tmp.putInteger(require); |
|
89 |
tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
90 |
false, TAG_REQUIRE), tmp); |
|
91 |
} |
|
92 |
if (inhibit != -1) { |
|
93 |
DerOutputStream tmp = new DerOutputStream(); |
|
94 |
tmp.putInteger(inhibit); |
|
95 |
tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
96 |
false, TAG_INHIBIT), tmp); |
|
97 |
} |
|
98 |
seq.write(DerValue.tag_Sequence, tagged); |
|
99 |
this.extensionValue = seq.toByteArray(); |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Create a PolicyConstraintsExtension object with both |
|
104 |
* require explicit policy and inhibit policy mapping. The |
|
105 |
* extension is marked non-critical. |
|
106 |
* |
|
107 |
* @param require require explicit policy (-1 for optional). |
|
108 |
* @param inhibit inhibit policy mapping (-1 for optional). |
|
109 |
*/ |
|
110 |
public PolicyConstraintsExtension(int require, int inhibit) |
|
111 |
throws IOException { |
|
28420
8fe17d073b54
8059916: Change default criticality of policy mappings and policy constraints certificate extensions
juh
parents:
25859
diff
changeset
|
112 |
this(Boolean.TRUE, require, inhibit); |
2 | 113 |
} |
114 |
||
115 |
/** |
|
116 |
* Create a PolicyConstraintsExtension object with specified |
|
117 |
* criticality and both require explicit policy and inhibit |
|
118 |
* policy mapping. |
|
119 |
* |
|
120 |
* @param critical true if the extension is to be treated as critical. |
|
121 |
* @param require require explicit policy (-1 for optional). |
|
122 |
* @param inhibit inhibit policy mapping (-1 for optional). |
|
123 |
*/ |
|
124 |
public PolicyConstraintsExtension(Boolean critical, int require, int inhibit) |
|
125 |
throws IOException { |
|
126 |
this.require = require; |
|
127 |
this.inhibit = inhibit; |
|
128 |
this.extensionId = PKIXExtensions.PolicyConstraints_Id; |
|
129 |
this.critical = critical.booleanValue(); |
|
130 |
encodeThis(); |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Create the extension from its DER encoded value and criticality. |
|
135 |
* |
|
136 |
* @param critical true if the extension is to be treated as critical. |
|
137 |
* @param value an array of DER encoded bytes of the actual value. |
|
138 |
* @exception ClassCastException if value is not an array of bytes |
|
139 |
* @exception IOException on error. |
|
140 |
*/ |
|
141 |
public PolicyConstraintsExtension(Boolean critical, Object value) |
|
142 |
throws IOException { |
|
143 |
this.extensionId = PKIXExtensions.PolicyConstraints_Id; |
|
144 |
this.critical = critical.booleanValue(); |
|
145 |
||
146 |
this.extensionValue = (byte[]) value; |
|
147 |
DerValue val = new DerValue(this.extensionValue); |
|
148 |
if (val.tag != DerValue.tag_Sequence) { |
|
149 |
throw new IOException("Sequence tag missing for PolicyConstraint."); |
|
150 |
} |
|
151 |
DerInputStream in = val.data; |
|
152 |
while (in != null && in.available() != 0) { |
|
153 |
DerValue next = in.getDerValue(); |
|
154 |
||
155 |
if (next.isContextSpecific(TAG_REQUIRE) && !next.isConstructed()) { |
|
156 |
if (this.require != -1) |
|
157 |
throw new IOException("Duplicate requireExplicitPolicy" + |
|
158 |
"found in the PolicyConstraintsExtension"); |
|
159 |
next.resetTag(DerValue.tag_Integer); |
|
160 |
this.require = next.getInteger(); |
|
161 |
||
162 |
} else if (next.isContextSpecific(TAG_INHIBIT) && |
|
163 |
!next.isConstructed()) { |
|
164 |
if (this.inhibit != -1) |
|
165 |
throw new IOException("Duplicate inhibitPolicyMapping" + |
|
166 |
"found in the PolicyConstraintsExtension"); |
|
167 |
next.resetTag(DerValue.tag_Integer); |
|
168 |
this.inhibit = next.getInteger(); |
|
169 |
} else |
|
170 |
throw new IOException("Invalid encoding of PolicyConstraint"); |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Return the extension as user readable string. |
|
176 |
*/ |
|
177 |
public String toString() { |
|
30649
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
178 |
StringBuilder sb = new StringBuilder(); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
179 |
sb.append(super.toString()) |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
180 |
.append("PolicyConstraints: [") |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
181 |
.append(" Require: "); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
182 |
if (require == -1) { |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
183 |
sb.append("unspecified;"); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
184 |
} else { |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
185 |
sb.append(require) |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
186 |
.append(';'); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
187 |
} |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
188 |
sb.append("\tInhibit: "); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
189 |
if (inhibit == -1) { |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
190 |
sb.append("unspecified"); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
191 |
} else { |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
192 |
sb.append(inhibit); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
193 |
} |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
194 |
sb.append(" ]\n"); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
28420
diff
changeset
|
195 |
return sb.toString(); |
2 | 196 |
} |
197 |
||
198 |
/** |
|
199 |
* Write the extension to the DerOutputStream. |
|
200 |
* |
|
201 |
* @param out the DerOutputStream to write the extension to. |
|
202 |
* @exception IOException on encoding errors. |
|
203 |
*/ |
|
204 |
public void encode(OutputStream out) throws IOException { |
|
205 |
DerOutputStream tmp = new DerOutputStream(); |
|
206 |
if (extensionValue == null) { |
|
207 |
extensionId = PKIXExtensions.PolicyConstraints_Id; |
|
28420
8fe17d073b54
8059916: Change default criticality of policy mappings and policy constraints certificate extensions
juh
parents:
25859
diff
changeset
|
208 |
critical = true; |
2 | 209 |
encodeThis(); |
210 |
} |
|
211 |
super.encode(tmp); |
|
212 |
out.write(tmp.toByteArray()); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Set the attribute value. |
|
217 |
*/ |
|
218 |
public void set(String name, Object obj) throws IOException { |
|
219 |
if (!(obj instanceof Integer)) { |
|
220 |
throw new IOException("Attribute value should be of type Integer."); |
|
221 |
} |
|
222 |
if (name.equalsIgnoreCase(REQUIRE)) { |
|
223 |
require = ((Integer)obj).intValue(); |
|
224 |
} else if (name.equalsIgnoreCase(INHIBIT)) { |
|
225 |
inhibit = ((Integer)obj).intValue(); |
|
226 |
} else { |
|
227 |
throw new IOException("Attribute name " + "[" + name + "]" + |
|
228 |
" not recognized by " + |
|
229 |
"CertAttrSet:PolicyConstraints."); |
|
230 |
} |
|
231 |
encodeThis(); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Get the attribute value. |
|
236 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
237 |
public Integer get(String name) throws IOException { |
2 | 238 |
if (name.equalsIgnoreCase(REQUIRE)) { |
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
10336
diff
changeset
|
239 |
return require; |
2 | 240 |
} else if (name.equalsIgnoreCase(INHIBIT)) { |
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
10336
diff
changeset
|
241 |
return inhibit; |
2 | 242 |
} else { |
243 |
throw new IOException("Attribute name not recognized by " + |
|
244 |
"CertAttrSet:PolicyConstraints."); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Delete the attribute value. |
|
250 |
*/ |
|
251 |
public void delete(String name) throws IOException { |
|
252 |
if (name.equalsIgnoreCase(REQUIRE)) { |
|
253 |
require = -1; |
|
254 |
} else if (name.equalsIgnoreCase(INHIBIT)) { |
|
255 |
inhibit = -1; |
|
256 |
} else { |
|
257 |
throw new IOException("Attribute name not recognized by " + |
|
258 |
"CertAttrSet:PolicyConstraints."); |
|
259 |
} |
|
260 |
encodeThis(); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Return an enumeration of names of attributes existing within this |
|
265 |
* attribute. |
|
266 |
*/ |
|
267 |
public Enumeration<String> getElements() { |
|
268 |
AttributeNameEnumeration elements = new AttributeNameEnumeration(); |
|
269 |
elements.addElement(REQUIRE); |
|
270 |
elements.addElement(INHIBIT); |
|
271 |
||
272 |
return (elements.elements()); |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Return the name of this attribute. |
|
277 |
*/ |
|
278 |
public String getName() { |
|
279 |
return (NAME); |
|
280 |
} |
|
281 |
} |