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.InvocationTargetException;
|
|
32 |
import java.security.cert.CRLException;
|
|
33 |
import java.security.cert.CertificateException;
|
|
34 |
import java.util.Collection;
|
|
35 |
import java.util.Enumeration;
|
|
36 |
import java.util.Hashtable;
|
|
37 |
|
|
38 |
import sun.security.util.*;
|
|
39 |
import sun.misc.HexDumpEncoder;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* This class defines the CRL Extensions.
|
|
43 |
* It is used for both CRL Extensions and CRL Entry Extensions,
|
|
44 |
* which are defined are follows:
|
|
45 |
* <pre>
|
|
46 |
* TBSCertList ::= SEQUENCE {
|
|
47 |
* version Version OPTIONAL, -- if present, must be v2
|
|
48 |
* signature AlgorithmIdentifier,
|
|
49 |
* issuer Name,
|
|
50 |
* thisUpdate Time,
|
|
51 |
* nextUpdate Time OPTIONAL,
|
|
52 |
* revokedCertificates SEQUENCE OF SEQUENCE {
|
|
53 |
* userCertificate CertificateSerialNumber,
|
|
54 |
* revocationDate Time,
|
|
55 |
* crlEntryExtensions Extensions OPTIONAL -- if present, must be v2
|
|
56 |
* } OPTIONAL,
|
|
57 |
* crlExtensions [0] EXPLICIT Extensions OPTIONAL -- if present, must be v2
|
|
58 |
* }
|
|
59 |
* </pre>
|
|
60 |
*
|
|
61 |
* @author Hemma Prafullchandra
|
|
62 |
*/
|
|
63 |
public class CRLExtensions {
|
|
64 |
|
|
65 |
private Hashtable<String,Extension> map = new Hashtable<String,Extension>();
|
|
66 |
private boolean unsupportedCritExt = false;
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Default constructor.
|
|
70 |
*/
|
|
71 |
public CRLExtensions() { }
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Create the object, decoding the values from the passed DER stream.
|
|
75 |
*
|
|
76 |
* @param in the DerInputStream to read the Extension from, i.e. the
|
|
77 |
* sequence of extensions.
|
|
78 |
* @exception CRLException on decoding errors.
|
|
79 |
*/
|
|
80 |
public CRLExtensions(DerInputStream in) throws CRLException {
|
|
81 |
init(in);
|
|
82 |
}
|
|
83 |
|
|
84 |
// helper routine
|
|
85 |
private void init(DerInputStream derStrm) throws CRLException {
|
|
86 |
try {
|
|
87 |
DerInputStream str = derStrm;
|
|
88 |
|
|
89 |
byte nextByte = (byte)derStrm.peekByte();
|
|
90 |
// check for context specific byte 0; skip it
|
|
91 |
if (((nextByte & 0x0c0) == 0x080) &&
|
|
92 |
((nextByte & 0x01f) == 0x000)) {
|
|
93 |
DerValue val = str.getDerValue();
|
|
94 |
str = val.data;
|
|
95 |
}
|
|
96 |
|
|
97 |
DerValue[] exts = str.getSequence(5);
|
|
98 |
for (int i = 0; i < exts.length; i++) {
|
|
99 |
Extension ext = new Extension(exts[i]);
|
|
100 |
parseExtension(ext);
|
|
101 |
}
|
|
102 |
} catch (IOException e) {
|
|
103 |
throw new CRLException("Parsing error: " + e.toString());
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
private static final Class[] PARAMS = {Boolean.class, Object.class};
|
|
108 |
|
|
109 |
// Parse the encoded extension
|
|
110 |
private void parseExtension(Extension ext) throws CRLException {
|
|
111 |
try {
|
|
112 |
Class extClass = OIDMap.getClass(ext.getExtensionId());
|
|
113 |
if (extClass == null) { // Unsupported extension
|
|
114 |
if (ext.isCritical())
|
|
115 |
unsupportedCritExt = true;
|
|
116 |
if (map.put(ext.getExtensionId().toString(), ext) != null)
|
|
117 |
throw new CRLException("Duplicate extensions not allowed");
|
|
118 |
return;
|
|
119 |
}
|
|
120 |
Constructor cons = ((Class<?>)extClass).getConstructor(PARAMS);
|
|
121 |
Object[] passed = new Object[] {Boolean.valueOf(ext.isCritical()),
|
|
122 |
ext.getExtensionValue()};
|
|
123 |
CertAttrSet crlExt = (CertAttrSet)cons.newInstance(passed);
|
|
124 |
if (map.put(crlExt.getName(), (Extension)crlExt) != null) {
|
|
125 |
throw new CRLException("Duplicate extensions not allowed");
|
|
126 |
}
|
|
127 |
} catch (InvocationTargetException invk) {
|
|
128 |
throw new CRLException(invk.getTargetException().getMessage());
|
|
129 |
} catch (Exception e) {
|
|
130 |
throw new CRLException(e.toString());
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Encode the extensions in DER form to the stream.
|
|
136 |
*
|
|
137 |
* @param out the DerOutputStream to marshal the contents to.
|
|
138 |
* @param isExplicit the tag indicating whether this is an entry
|
|
139 |
* extension (false) or a CRL extension (true).
|
|
140 |
* @exception CRLException on encoding errors.
|
|
141 |
*/
|
|
142 |
public void encode(OutputStream out, boolean isExplicit)
|
|
143 |
throws CRLException {
|
|
144 |
try {
|
|
145 |
DerOutputStream extOut = new DerOutputStream();
|
|
146 |
Collection<Extension> allExts = map.values();
|
|
147 |
Object[] objs = allExts.toArray();
|
|
148 |
|
|
149 |
for (int i = 0; i < objs.length; i++) {
|
|
150 |
if (objs[i] instanceof CertAttrSet)
|
|
151 |
((CertAttrSet)objs[i]).encode(extOut);
|
|
152 |
else if (objs[i] instanceof Extension)
|
|
153 |
((Extension)objs[i]).encode(extOut);
|
|
154 |
else
|
|
155 |
throw new CRLException("Illegal extension object");
|
|
156 |
}
|
|
157 |
|
|
158 |
DerOutputStream seq = new DerOutputStream();
|
|
159 |
seq.write(DerValue.tag_Sequence, extOut);
|
|
160 |
|
|
161 |
DerOutputStream tmp = new DerOutputStream();
|
|
162 |
if (isExplicit)
|
|
163 |
tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT,
|
|
164 |
true, (byte)0), seq);
|
|
165 |
else
|
|
166 |
tmp = seq;
|
|
167 |
|
|
168 |
out.write(tmp.toByteArray());
|
|
169 |
} catch (IOException e) {
|
|
170 |
throw new CRLException("Encoding error: " + e.toString());
|
|
171 |
} catch (CertificateException e) {
|
|
172 |
throw new CRLException("Encoding error: " + e.toString());
|
|
173 |
}
|
|
174 |
}
|
|
175 |
|
|
176 |
/**
|
|
177 |
* Get the extension with this alias.
|
|
178 |
*
|
|
179 |
* @param alias the identifier string for the extension to retrieve.
|
|
180 |
*/
|
|
181 |
public Extension get(String alias) {
|
|
182 |
X509AttributeName attr = new X509AttributeName(alias);
|
|
183 |
String name;
|
|
184 |
String id = attr.getPrefix();
|
|
185 |
if (id.equalsIgnoreCase(X509CertImpl.NAME)) { // fully qualified
|
|
186 |
int index = alias.lastIndexOf(".");
|
|
187 |
name = alias.substring(index + 1);
|
|
188 |
} else
|
|
189 |
name = alias;
|
|
190 |
return map.get(name);
|
|
191 |
}
|
|
192 |
|
|
193 |
/**
|
|
194 |
* Set the extension value with this alias.
|
|
195 |
*
|
|
196 |
* @param alias the identifier string for the extension to set.
|
|
197 |
* @param obj the Object to set the extension identified by the
|
|
198 |
* alias.
|
|
199 |
*/
|
|
200 |
public void set(String alias, Object obj) {
|
|
201 |
map.put(alias, (Extension)obj);
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Delete the extension value with this alias.
|
|
206 |
*
|
|
207 |
* @param alias the identifier string for the extension to delete.
|
|
208 |
*/
|
|
209 |
public void delete(String alias) {
|
|
210 |
map.remove(alias);
|
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* Return an enumeration of the extensions.
|
|
215 |
* @return an enumeration of the extensions in this CRL.
|
|
216 |
*/
|
|
217 |
public Enumeration<Extension> getElements() {
|
|
218 |
return map.elements();
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* Return a collection view of the extensions.
|
|
223 |
* @return a collection view of the extensions in this CRL.
|
|
224 |
*/
|
|
225 |
public Collection<Extension> getAllExtensions() {
|
|
226 |
return map.values();
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Return true if a critical extension is found that is
|
|
231 |
* not supported, otherwise return false.
|
|
232 |
*/
|
|
233 |
public boolean hasUnsupportedCriticalExtension() {
|
|
234 |
return unsupportedCritExt;
|
|
235 |
}
|
|
236 |
|
|
237 |
/**
|
|
238 |
* Compares this CRLExtensions for equality with the specified
|
|
239 |
* object. If the <code>other</code> object is an
|
|
240 |
* <code>instanceof</code> <code>CRLExtensions</code>, then
|
|
241 |
* all the entries are compared with the entries from this.
|
|
242 |
*
|
|
243 |
* @param other the object to test for equality with this CRLExtensions.
|
|
244 |
* @return true iff all the entries match that of the Other,
|
|
245 |
* false otherwise.
|
|
246 |
*/
|
|
247 |
public boolean equals(Object other) {
|
|
248 |
if (this == other)
|
|
249 |
return true;
|
|
250 |
if (!(other instanceof CRLExtensions))
|
|
251 |
return false;
|
|
252 |
Collection<Extension> otherC =
|
|
253 |
((CRLExtensions)other).getAllExtensions();
|
|
254 |
Object[] objs = otherC.toArray();
|
|
255 |
|
|
256 |
int len = objs.length;
|
|
257 |
if (len != map.size())
|
|
258 |
return false;
|
|
259 |
|
|
260 |
Extension otherExt, thisExt;
|
|
261 |
String key = null;
|
|
262 |
for (int i = 0; i < len; i++) {
|
|
263 |
if (objs[i] instanceof CertAttrSet)
|
|
264 |
key = ((CertAttrSet)objs[i]).getName();
|
|
265 |
otherExt = (Extension)objs[i];
|
|
266 |
if (key == null)
|
|
267 |
key = otherExt.getExtensionId().toString();
|
|
268 |
thisExt = map.get(key);
|
|
269 |
if (thisExt == null)
|
|
270 |
return false;
|
|
271 |
if (! thisExt.equals(otherExt))
|
|
272 |
return false;
|
|
273 |
}
|
|
274 |
return true;
|
|
275 |
}
|
|
276 |
|
|
277 |
/**
|
|
278 |
* Returns a hashcode value for this CRLExtensions.
|
|
279 |
*
|
|
280 |
* @return the hashcode value.
|
|
281 |
*/
|
|
282 |
public int hashCode() {
|
|
283 |
return map.hashCode();
|
|
284 |
}
|
|
285 |
|
|
286 |
/**
|
|
287 |
* Returns a string representation of this <tt>CRLExtensions</tt> object
|
|
288 |
* in the form of a set of entries, enclosed in braces and separated
|
|
289 |
* by the ASCII characters "<tt>, </tt>" (comma and space).
|
|
290 |
* <p>Overrides to <tt>toString</tt> method of <tt>Object</tt>.
|
|
291 |
*
|
|
292 |
* @return a string representation of this CRLExtensions.
|
|
293 |
*/
|
|
294 |
public String toString() {
|
|
295 |
return map.toString();
|
|
296 |
}
|
|
297 |
}
|