2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.x509;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.OutputStream;
|
|
30 |
import java.lang.reflect.Constructor;
|
|
31 |
import java.lang.reflect.Field;
|
|
32 |
import java.lang.reflect.InvocationTargetException;
|
|
33 |
import java.security.cert.CertificateException;
|
|
34 |
import java.util.*;
|
|
35 |
|
|
36 |
import sun.misc.HexDumpEncoder;
|
|
37 |
|
|
38 |
import sun.security.util.*;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* This class defines the Extensions attribute for the Certificate.
|
|
42 |
*
|
|
43 |
* @author Amit Kapoor
|
|
44 |
* @author Hemma Prafullchandra
|
|
45 |
* @see CertAttrSet
|
|
46 |
*/
|
|
47 |
public class CertificateExtensions implements CertAttrSet<Extension> {
|
|
48 |
/**
|
|
49 |
* Identifier for this attribute, to be used with the
|
|
50 |
* get, set, delete methods of Certificate, x509 type.
|
|
51 |
*/
|
|
52 |
public static final String IDENT = "x509.info.extensions";
|
|
53 |
/**
|
|
54 |
* name
|
|
55 |
*/
|
|
56 |
public static final String NAME = "extensions";
|
|
57 |
|
|
58 |
private static final Debug debug = Debug.getInstance("x509");
|
|
59 |
|
|
60 |
private Hashtable<String,Extension> map = new Hashtable<String,Extension>();
|
|
61 |
private boolean unsupportedCritExt = false;
|
|
62 |
|
|
63 |
private Map<String,Extension> unparseableExtensions;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Default constructor.
|
|
67 |
*/
|
|
68 |
public CertificateExtensions() { }
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Create the object, decoding the values from the passed DER stream.
|
|
72 |
*
|
|
73 |
* @param in the DerInputStream to read the Extension from.
|
|
74 |
* @exception IOException on decoding errors.
|
|
75 |
*/
|
|
76 |
public CertificateExtensions(DerInputStream in) throws IOException {
|
|
77 |
init(in);
|
|
78 |
}
|
|
79 |
|
|
80 |
// helper routine
|
|
81 |
private void init(DerInputStream in) throws IOException {
|
|
82 |
|
|
83 |
DerValue[] exts = in.getSequence(5);
|
|
84 |
|
|
85 |
for (int i = 0; i < exts.length; i++) {
|
|
86 |
Extension ext = new Extension(exts[i]);
|
|
87 |
parseExtension(ext);
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
private static Class[] PARAMS = {Boolean.class, Object.class};
|
|
92 |
|
|
93 |
// Parse the encoded extension
|
|
94 |
private void parseExtension(Extension ext) throws IOException {
|
|
95 |
try {
|
|
96 |
Class extClass = OIDMap.getClass(ext.getExtensionId());
|
|
97 |
if (extClass == null) { // Unsupported extension
|
|
98 |
if (ext.isCritical()) {
|
|
99 |
unsupportedCritExt = true;
|
|
100 |
}
|
|
101 |
if (map.put(ext.getExtensionId().toString(), ext) == null) {
|
|
102 |
return;
|
|
103 |
} else {
|
|
104 |
throw new IOException("Duplicate extensions not allowed");
|
|
105 |
}
|
|
106 |
}
|
|
107 |
Constructor cons = ((Class<?>)extClass).getConstructor(PARAMS);
|
|
108 |
|
|
109 |
Object[] passed = new Object[] {Boolean.valueOf(ext.isCritical()),
|
|
110 |
ext.getExtensionValue()};
|
|
111 |
CertAttrSet certExt = (CertAttrSet)cons.newInstance(passed);
|
|
112 |
if (map.put(certExt.getName(), (Extension)certExt) != null) {
|
|
113 |
throw new IOException("Duplicate extensions not allowed");
|
|
114 |
}
|
|
115 |
} catch (InvocationTargetException invk) {
|
|
116 |
Throwable e = invk.getTargetException();
|
|
117 |
if (ext.isCritical() == false) {
|
|
118 |
// ignore errors parsing non-critical extensions
|
|
119 |
if (unparseableExtensions == null) {
|
|
120 |
unparseableExtensions = new HashMap<String,Extension>();
|
|
121 |
}
|
|
122 |
unparseableExtensions.put(ext.getExtensionId().toString(),
|
|
123 |
new UnparseableExtension(ext, e));
|
|
124 |
if (debug != null) {
|
|
125 |
debug.println("Error parsing extension: " + ext);
|
|
126 |
e.printStackTrace();
|
|
127 |
HexDumpEncoder h = new HexDumpEncoder();
|
|
128 |
System.err.println(h.encodeBuffer(ext.getExtensionValue()));
|
|
129 |
}
|
|
130 |
return;
|
|
131 |
}
|
|
132 |
if (e instanceof IOException) {
|
|
133 |
throw (IOException)e;
|
|
134 |
} else {
|
|
135 |
throw (IOException)new IOException(e.toString()).initCause(e);
|
|
136 |
}
|
|
137 |
} catch (IOException e) {
|
|
138 |
throw e;
|
|
139 |
} catch (Exception e) {
|
|
140 |
throw (IOException)new IOException(e.toString()).initCause(e);
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Encode the extensions in DER form to the stream, setting
|
|
146 |
* the context specific tag as needed in the X.509 v3 certificate.
|
|
147 |
*
|
|
148 |
* @param out the DerOutputStream to marshal the contents to.
|
|
149 |
* @exception CertificateException on encoding errors.
|
|
150 |
* @exception IOException on errors.
|
|
151 |
*/
|
|
152 |
public void encode(OutputStream out)
|
|
153 |
throws CertificateException, IOException {
|
|
154 |
encode(out, false);
|
|
155 |
}
|
|
156 |
|
|
157 |
/**
|
|
158 |
* Encode the extensions in DER form to the stream.
|
|
159 |
*
|
|
160 |
* @param out the DerOutputStream to marshal the contents to.
|
|
161 |
* @param isCertReq if true then no context specific tag is added.
|
|
162 |
* @exception CertificateException on encoding errors.
|
|
163 |
* @exception IOException on errors.
|
|
164 |
*/
|
|
165 |
public void encode(OutputStream out, boolean isCertReq)
|
|
166 |
throws CertificateException, IOException {
|
|
167 |
DerOutputStream extOut = new DerOutputStream();
|
|
168 |
Collection<Extension> allExts = map.values();
|
|
169 |
Object[] objs = allExts.toArray();
|
|
170 |
|
|
171 |
for (int i = 0; i < objs.length; i++) {
|
|
172 |
if (objs[i] instanceof CertAttrSet)
|
|
173 |
((CertAttrSet)objs[i]).encode(extOut);
|
|
174 |
else if (objs[i] instanceof Extension)
|
|
175 |
((Extension)objs[i]).encode(extOut);
|
|
176 |
else
|
|
177 |
throw new CertificateException("Illegal extension object");
|
|
178 |
}
|
|
179 |
|
|
180 |
DerOutputStream seq = new DerOutputStream();
|
|
181 |
seq.write(DerValue.tag_Sequence, extOut);
|
|
182 |
|
|
183 |
DerOutputStream tmp;
|
|
184 |
if (!isCertReq) { // certificate
|
|
185 |
tmp = new DerOutputStream();
|
|
186 |
tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)3),
|
|
187 |
seq);
|
|
188 |
} else
|
|
189 |
tmp = seq; // pkcs#10 certificateRequest
|
|
190 |
|
|
191 |
out.write(tmp.toByteArray());
|
|
192 |
}
|
|
193 |
|
|
194 |
/**
|
|
195 |
* Set the attribute value.
|
|
196 |
* @param name the extension name used in the cache.
|
|
197 |
* @param obj the object to set.
|
|
198 |
* @exception IOException if the object could not be cached.
|
|
199 |
*/
|
|
200 |
public void set(String name, Object obj) throws IOException {
|
|
201 |
if (obj instanceof Extension) {
|
|
202 |
map.put(name, (Extension)obj);
|
|
203 |
} else {
|
|
204 |
throw new IOException("Unknown extension type.");
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Get the attribute value.
|
|
210 |
* @param name the extension name used in the lookup.
|
|
211 |
* @exception IOException if named extension is not found.
|
|
212 |
*/
|
|
213 |
public Object get(String name) throws IOException {
|
|
214 |
Object obj = map.get(name);
|
|
215 |
if (obj == null) {
|
|
216 |
throw new IOException("No extension found with name " + name);
|
|
217 |
}
|
|
218 |
return (obj);
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* Delete the attribute value.
|
|
223 |
* @param name the extension name used in the lookup.
|
|
224 |
* @exception IOException if named extension is not found.
|
|
225 |
*/
|
|
226 |
public void delete(String name) throws IOException {
|
|
227 |
Object obj = map.get(name);
|
|
228 |
if (obj == null) {
|
|
229 |
throw new IOException("No extension found with name " + name);
|
|
230 |
}
|
|
231 |
map.remove(name);
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Return an enumeration of names of attributes existing within this
|
|
236 |
* attribute.
|
|
237 |
*/
|
|
238 |
public Enumeration<Extension> getElements() {
|
|
239 |
return map.elements();
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Return a collection view of the extensions.
|
|
244 |
* @return a collection view of the extensions in this Certificate.
|
|
245 |
*/
|
|
246 |
public Collection<Extension> getAllExtensions() {
|
|
247 |
return map.values();
|
|
248 |
}
|
|
249 |
|
|
250 |
public Map<String,Extension> getUnparseableExtensions() {
|
|
251 |
if (unparseableExtensions == null) {
|
|
252 |
return Collections.emptyMap();
|
|
253 |
} else {
|
|
254 |
return unparseableExtensions;
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Return the name of this attribute.
|
|
260 |
*/
|
|
261 |
public String getName() {
|
|
262 |
return NAME;
|
|
263 |
}
|
|
264 |
|
|
265 |
/**
|
|
266 |
* Return true if a critical extension is found that is
|
|
267 |
* not supported, otherwise return false.
|
|
268 |
*/
|
|
269 |
public boolean hasUnsupportedCriticalExtension() {
|
|
270 |
return unsupportedCritExt;
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* Compares this CertificateExtensions for equality with the specified
|
|
275 |
* object. If the <code>other</code> object is an
|
|
276 |
* <code>instanceof</code> <code>CertificateExtensions</code>, then
|
|
277 |
* all the entries are compared with the entries from this.
|
|
278 |
*
|
|
279 |
* @param other the object to test for equality with this
|
|
280 |
* CertificateExtensions.
|
|
281 |
* @return true iff all the entries match that of the Other,
|
|
282 |
* false otherwise.
|
|
283 |
*/
|
|
284 |
public boolean equals(Object other) {
|
|
285 |
if (this == other)
|
|
286 |
return true;
|
|
287 |
if (!(other instanceof CertificateExtensions))
|
|
288 |
return false;
|
|
289 |
Collection<Extension> otherC =
|
|
290 |
((CertificateExtensions)other).getAllExtensions();
|
|
291 |
Object[] objs = otherC.toArray();
|
|
292 |
|
|
293 |
int len = objs.length;
|
|
294 |
if (len != map.size())
|
|
295 |
return false;
|
|
296 |
|
|
297 |
Extension otherExt, thisExt;
|
|
298 |
String key = null;
|
|
299 |
for (int i = 0; i < len; i++) {
|
|
300 |
if (objs[i] instanceof CertAttrSet)
|
|
301 |
key = ((CertAttrSet)objs[i]).getName();
|
|
302 |
otherExt = (Extension)objs[i];
|
|
303 |
if (key == null)
|
|
304 |
key = otherExt.getExtensionId().toString();
|
|
305 |
thisExt = map.get(key);
|
|
306 |
if (thisExt == null)
|
|
307 |
return false;
|
|
308 |
if (! thisExt.equals(otherExt))
|
|
309 |
return false;
|
|
310 |
}
|
|
311 |
return this.getUnparseableExtensions().equals(
|
|
312 |
((CertificateExtensions)other).getUnparseableExtensions());
|
|
313 |
}
|
|
314 |
|
|
315 |
/**
|
|
316 |
* Returns a hashcode value for this CertificateExtensions.
|
|
317 |
*
|
|
318 |
* @return the hashcode value.
|
|
319 |
*/
|
|
320 |
public int hashCode() {
|
|
321 |
return map.hashCode() + getUnparseableExtensions().hashCode();
|
|
322 |
}
|
|
323 |
|
|
324 |
/**
|
|
325 |
* Returns a string representation of this <tt>CertificateExtensions</tt>
|
|
326 |
* object in the form of a set of entries, enclosed in braces and separated
|
|
327 |
* by the ASCII characters "<tt>, </tt>" (comma and space).
|
|
328 |
* <p>Overrides to <tt>toString</tt> method of <tt>Object</tt>.
|
|
329 |
*
|
|
330 |
* @return a string representation of this CertificateExtensions.
|
|
331 |
*/
|
|
332 |
public String toString() {
|
|
333 |
return map.toString();
|
|
334 |
}
|
|
335 |
|
|
336 |
}
|
|
337 |
|
|
338 |
class UnparseableExtension extends Extension {
|
|
339 |
private String name;
|
|
340 |
private Throwable why;
|
|
341 |
|
|
342 |
public UnparseableExtension(Extension ext, Throwable why) {
|
|
343 |
super(ext);
|
|
344 |
|
|
345 |
name = "";
|
|
346 |
try {
|
|
347 |
Class extClass = OIDMap.getClass(ext.getExtensionId());
|
|
348 |
if (extClass != null) {
|
|
349 |
Field field = extClass.getDeclaredField("NAME");
|
|
350 |
name = (String)(field.get(null)) + " ";
|
|
351 |
}
|
|
352 |
} catch (Exception e) {
|
|
353 |
// If we cannot find the name, just ignore it
|
|
354 |
}
|
|
355 |
|
|
356 |
this.why = why;
|
|
357 |
}
|
|
358 |
|
|
359 |
@Override public String toString() {
|
|
360 |
return super.toString() +
|
|
361 |
"Unparseable " + name + "extension due to\n" + why + "\n\n" +
|
|
362 |
new sun.misc.HexDumpEncoder().encodeBuffer(getExtensionValue());
|
|
363 |
}
|
|
364 |
}
|