Extension | Line # 90 | 40 | 8 | 50.7% |
0.5068493
|
(1) | |||
Result | |||
0.5068493
|
issrg.test.ds.TestDS.testIssuing issrg.test.ds.TestDS.testIssuing | 1 PASS | |
1 | /* | |
2 | * Copyright (c) 2006, University of Kent | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * 1. Neither the name of the University of Kent nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | |
20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
22 | * PURPOSE ARE DISCLAIMED. | |
23 | * | |
24 | * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
31 | * POSSIBILITY OF SUCH DAMAGE. | |
32 | * | |
33 | * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE | |
34 | * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS | |
35 | * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS | |
36 | * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH | |
37 | * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH | |
38 | * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY | |
39 | * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE | |
40 | * SERIOUS FAULTS, IN THIS SOFTWARE. | |
41 | * | |
42 | * 5. This license is governed, except to the extent that local laws | |
43 | * necessarily apply, by the laws of England and Wales. | |
44 | */ | |
45 | /* | |
46 | * Copyright (c) 2000-2005, University of Salford | |
47 | * All rights reserved. | |
48 | * | |
49 | * Redistribution and use in source and binary forms, with or without | |
50 | * modification, are permitted provided that the following conditions are met: | |
51 | * | |
52 | * Redistributions of source code must retain the above copyright notice, this | |
53 | * list of conditions and the following disclaimer. | |
54 | * | |
55 | * Redistributions in binary form must reproduce the above copyright notice, | |
56 | * this list of conditions and the following disclaimer in the documentation | |
57 | * and/or other materials provided with the distribution. | |
58 | * | |
59 | * Neither the name of the University of Salford nor the names of its | |
60 | * contributors may be used to endorse or promote products derived from this | |
61 | * software without specific prior written permission. | |
62 | * | |
63 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
64 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
65 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
66 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
67 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
68 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
69 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
70 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
71 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
72 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
73 | * POSSIBILITY OF SUCH DAMAGE. | |
74 | */ | |
75 | ||
76 | package issrg.ac; | |
77 | ||
78 | import iaik.asn1.*; | |
79 | import iaik.asn1.structures.*; | |
80 | ||
81 | /** | |
82 | * This is the class representing the Extension ASN.1 construct. It contains an | |
83 | * OCTET_STRING | |
84 | * of the extension value, but the derived classes may parse that value to | |
85 | * represent a meaningful | |
86 | * structure instead, but they don't need to worry about encoding the value as | |
87 | * OCTET_STRING. | |
88 | */ | |
89 | ||
90 | public class Extension implements ASN1Type, Cloneable{ | |
91 | ||
92 | protected String extnID = ""; | |
93 | ||
94 | /** | |
95 | * Returns the OID of the extension in the dotted form. | |
96 | */ | |
97 | 0 | public String getExtensionOID(){ |
98 | 0 | return extnID; |
99 | } | |
100 | ||
101 | /** | |
102 | * Sets the OID of the extension in the dotted form. | |
103 | */ | |
104 | 0 | public void setExtensionOID(String oid){ |
105 | 0 | extnID=oid; |
106 | } | |
107 | ||
108 | protected boolean critical = false; // optional | |
109 | ||
110 | /** | |
111 | * Returns true if the extension is critical. | |
112 | */ | |
113 | 506 | public boolean isCritical(){ |
114 | 506 | return critical; |
115 | } | |
116 | ||
117 | /** | |
118 | * Sets the flag if the extension is critical. | |
119 | */ | |
120 | 0 | public void setCritical(boolean critical){ |
121 | 0 | this.critical=critical; |
122 | } | |
123 | ||
124 | protected ASN1Object theObject = null; | |
125 | protected static final NULL Null = new NULL(); | |
126 | ||
127 | /** | |
128 | * Returns the OCTET STRING of the extnValue. This method calls encodeValue() | |
129 | * to get the ASN1Object of the value, which is then DER-encoded and the | |
130 | * encoding is converted into OCTET STRING. | |
131 | * | |
132 | * <p>The descendant classes should override encodeValue() method to return | |
133 | * the correct ASN1Object representing the extension value. | |
134 | */ | |
135 | 120 | public final OCTET_STRING getExtensionValue() throws CodingException { |
136 | 120 | return new OCTET_STRING(iaik.asn1.DerCoder.encode(encodeValue())); |
137 | } | |
138 | ||
139 | /** | |
140 | * Sets the string of octets containing the extension value. This method | |
141 | * uses BER decoder to decode the bytes of the OCTET STRING, then the | |
142 | * resulting ASN1Object is passed to decodeValue() method. | |
143 | * | |
144 | * <p>The subclasses should override decodeValue() method to fill the | |
145 | * extension with meaning. | |
146 | */ | |
147 | 3116 | public final void setExtensionValue(OCTET_STRING value) throws CodingException { |
148 | 3116 | decodeValue(iaik.asn1.DerCoder.decode((byte[])value.getValue())); |
149 | } | |
150 | ||
151 | ||
152 | 0 | protected Extension() { |
153 | } | |
154 | ||
155 | /** | |
156 | * This constructor creates a copy of the given Extension. | |
157 | * | |
158 | * @param e - the Extension to copy | |
159 | */ | |
160 | 0 | public Extension(Extension e) { |
161 | 0 | extnID = new String(e.extnID); |
162 | 0 | critical = e.critical; |
163 | 0 | try{ |
164 | 0 | setExtensionValue(e.getExtensionValue()); |
165 | }catch(CodingException ce){ | |
166 | // this shouldn't happen | |
167 | } | |
168 | } | |
169 | ||
170 | /** | |
171 | * This constructor builds an Extension with no value set. | |
172 | * | |
173 | * @param extnID - the OID of the Extension | |
174 | * @param critical - the criticality flag; if true, the extension will be | |
175 | * marked critical; otherwise non-critical | |
176 | */ | |
177 | 41 | public Extension(String extnID, boolean critical){ |
178 | 41 | this.extnID = extnID; |
179 | 41 | this.critical = critical; |
180 | } | |
181 | ||
182 | /** | |
183 | * This constructor builds an Extension given the OID of the extension, | |
184 | * its criticality flag and the ASN1Object of the Extension Value. | |
185 | * | |
186 | * @param extnID - the OID of the extension | |
187 | * @param critical - the criticality flag; if true, the extension is critical | |
188 | * @param extnValue - the OCTET_STRING of the extension value; can be null; | |
189 | */ | |
190 | 0 | public Extension(String extnID, boolean critical, OCTET_STRING extnValue) throws CodingException{ |
191 | 0 | this(extnID, critical); |
192 | 0 | if (extnValue!=null) setExtensionValue(extnValue); |
193 | } | |
194 | ||
195 | /** | |
196 | * This constructor builds an Extension from its ASN1Object. | |
197 | * | |
198 | * @param ao - the ASN1Object of this Extension | |
199 | */ | |
200 | 3116 | public Extension(ASN1Object ao) throws CodingException{ |
201 | 3116 | decode(ao); |
202 | } | |
203 | ||
204 | /** | |
205 | * This method returns the ASN1Object of the Extension. It is a SEQUENCE | |
206 | * of ObjectID, criticality flag (if critical), and the OCTET STRING of the | |
207 | * extension value, as returned by getExtensionValue() method (which uses | |
208 | * encodeValue()). | |
209 | */ | |
210 | 120 | public ASN1Object toASN1Object() throws CodingException{ |
211 | 120 | ASN1Object result = new SEQUENCE(); |
212 | ||
213 | 120 | result.addComponent(new ObjectID(extnID)); |
214 | 120 | if (critical){ |
215 | 8 | result.addComponent(new BOOLEAN(critical)); |
216 | } | |
217 | 120 | result.addComponent(getExtensionValue()); |
218 | ||
219 | 120 | return result; |
220 | } | |
221 | ||
222 | /** | |
223 | * This method decodes a given ASN1Object. It must be a SEQUENCE of ObjectID, | |
224 | * boolean criticality flag, and the Extension value, which is passed to | |
225 | * setExtensionValue() method (which in its turn calls decodeValue() method). | |
226 | */ | |
227 | 3116 | public void decode(ASN1Object ao) throws CodingException{ |
228 | 3116 | if (!ao.isA(ASN.SEQUENCE)){ |
229 | 0 | throw new CodingException("Extension SEQUENCE tag was expected"); |
230 | } | |
231 | ||
232 | 3116 | extnID = (String)(((ObjectID)ao.getComponentAt(0)).getValue()); |
233 | ||
234 | 3116 | critical = false; // DEFAULT |
235 | 3116 | if (ao.countComponents()==3){ // of course, i could just get a type of the next entry, but this looks simplier |
236 | /** | |
237 | * TODO: class cast exception at this point is possible | |
238 | */ | |
239 | 631 | critical = ((Boolean)((BOOLEAN)ao.getComponentAt(1)).getValue()).booleanValue(); |
240 | }else{ | |
241 | 2485 | if (ao.countComponents()!=2){ |
242 | 0 | throw new CodingException("Illegal number of entries in Extension tag: "+Integer.toString(ao.countComponents())); |
243 | } | |
244 | } | |
245 | ||
246 | // anyway, this is the last entry | |
247 | 3116 | ao = ao.getComponentAt(ao.countComponents()-1); |
248 | 3116 | if (!ao.isA(ASN.OCTET_STRING)){ |
249 | 0 | throw new CodingException("OCTET STRING was expected as extnValue, but "+ao.getClass()+" was found"); |
250 | } | |
251 | 3116 | setExtensionValue((OCTET_STRING)ao); |
252 | } | |
253 | ||
254 | /** | |
255 | * This method decodes the Extension Value. By default it does not do | |
256 | * anything. | |
257 | * | |
258 | * <p>Override this method so that your Extension decodes what you want to | |
259 | * fill | |
260 | * in its internal fields. | |
261 | * | |
262 | * @param ao - the ASN1Object obtained by BER-decoding the bytes of the | |
263 | * extension value OCTET STRING | |
264 | */ | |
265 | 631 | public void decodeValue(ASN1Object ao) throws CodingException { |
266 | 631 | theObject=ao; |
267 | } | |
268 | ||
269 | /** | |
270 | * This method encodes the extension value to the ASN1Object representing the | |
271 | * encoding of the extension | |
272 | * value and returns it. By default it returns the contents of the Extension | |
273 | * that were received in decodeValue. | |
274 | * | |
275 | * <p>Override this method so that your Extension returns what you want. | |
276 | * | |
277 | * @return ASN1Object, which will be subsequently DER-encoded and the bytes | |
278 | * of that encoding will be converted into OCTET STRING of the extension | |
279 | * value | |
280 | */ | |
281 | 0 | public ASN1Object encodeValue() throws CodingException { |
282 | 0 | return theObject; |
283 | } | |
284 | ||
285 | /** | |
286 | * This method returns the String representation of the extension value. By | |
287 | * default it calls toString() method on extnValue object. | |
288 | * | |
289 | * <p>Override this method if you need more user-friendly output of your | |
290 | * custom extension value. | |
291 | * | |
292 | * @param indent - the indentation to use at the beginning of each line to | |
293 | * make the text human-friendly | |
294 | */ | |
295 | 0 | public String extensionValueToString(String indent){ |
296 | 0 | return theObject.toString(); |
297 | } | |
298 | ||
299 | 0 | public String toString(){ |
300 | 0 | return toString(""); |
301 | } | |
302 | ||
303 | 0 | public String toString(String ident){ |
304 | 0 | return "SEQUENCE { -- Extension --\n "+ident+ |
305 | "extnID = "+extnID+",\n "+ident+ | |
306 | 0 | "critical = "+(critical?"True":"False -- DEFAULT --")+",\n "+ident+ |
307 | "extn Value = "+extensionValueToString(ident)+"\n"+ident+"}"; | |
308 | } | |
309 | ||
310 | ||
311 | 0 | public Object clone(){ |
312 | 0 | return new Extension(this); |
313 | } | |
314 | } |
|