Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
121   509   30   4.32
54   244   0.47   28
28     2.04  
1    
 
 
  AttributeCertificateInfo       Line # 88 121 30 61.1% 0.61083746
 
  (1)
 
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    import java.math.BigInteger;
81    import java.util.Vector;
82    import java.util.Enumeration;
83   
84    /**
85    * This class represents the AttributeCertificateInfo ASN.1 construct.
86    */
87   
 
88    public class AttributeCertificateInfo implements ASN1Type, Cloneable{
89   
90    protected AttCertVersion version = new AttCertVersion();
91   
92    /**
93    * Returns the version of the AC.
94    */
 
95  0 toggle public AttCertVersion getVersion(){
96  0 return version;
97    }
98   
99    /**
100    * Sets the version of the AC.
101    */
 
102  0 toggle public void setVersion(AttCertVersion version){
103  0 this.version=version;
104    }
105   
106    protected Holder holder = new Holder();
107   
108    /**
109    * Returns the Holder construct of the AC.
110    */
 
111  7194 toggle public Holder getHolder(){
112  7194 return holder;
113    }
114   
115    /**
116    * Sets the Holder construct of the AC.
117    */
 
118  0 toggle public void setHolder(Holder holder){
119  0 this.holder=holder;
120    }
121   
122    protected AttCertIssuer issuer = new AttCertIssuer();
123   
124    /**
125    * Returns the AttCertIssuer construct of the AC.
126    */
 
127  3588 toggle public AttCertIssuer getIssuer(){
128  3588 return issuer;
129    }
130   
131    /**
132    * Sets the AttCertIssuer construct of the AC.
133    */
 
134  0 toggle public void setIssuer(AttCertIssuer issuer){
135  0 this.issuer=issuer;
136    }
137   
138    protected AlgorithmID signature = new AlgorithmID(); // Algorithm Identifier
139   
140    /**
141    * Returns the signature AlgorithmID construct of the AC.
142    */
 
143  0 toggle public AlgorithmID getSignatureAlgorithmID(){
144  0 return signature;
145    }
146   
147    /**
148    * Sets the signature AlgorithmID construct of the AC.
149    */
 
150  0 toggle public void setSignatureAlgorithmID(AlgorithmID signature){
151  0 this.signature=signature;
152    }
153   
154   
155    protected BigInteger serialNumber = new BigInteger("0");
156   
157    /**
158    * Returns the Serial Number of the AC.
159    */
 
160  1 toggle public BigInteger getSerialNumber(){
161  1 return serialNumber;
162    }
163   
164    /**
165    * Sets the Serial Number of the AC.
166    */
 
167  0 toggle public void setSerialNumber(BigInteger serialNumber){
168  0 this.serialNumber=serialNumber;
169    }
170   
171   
172    protected AttCertValidityPeriod attrCertValidityPeriod = new AttCertValidityPeriod();
173   
174    /**
175    * Returns the Attribute Certificate Validity Period construct of the AC.
176    */
 
177  3598 toggle public AttCertValidityPeriod getValidityPeriod(){
178  3598 return attrCertValidityPeriod;
179    }
180   
181    /**
182    * Sets the Attribute Certificate Validity Period construct of the AC.
183    */
 
184  0 toggle public void setValidityPeriod(AttCertValidityPeriod validityPeriod){
185  0 this.attrCertValidityPeriod=validityPeriod;
186    }
187   
188    protected Vector attributes = new Vector(); // mind you: X.509 does not allow to have several attributes of the same type
189    // one Attribute type - many values, if needed.
190    // checks are provided and exceptions are thrown.
191    /**
192    * Returns the set of Attributes of the AC. Each element of the Vector is an
193    * Attribute.
194    */
 
195  1813 toggle public Vector getAttributes(){
196  1813 return attributes;
197    }
198   
199    /**
200    * Sets the set of Attributes of the AC. Each element of the Vector should
201    * be an Attribute.
202    */
 
203  0 toggle public void setAttributes(Vector attributes){
204  0 this.attributes=attributes;
205    }
206   
207    protected BIT_STRING issuerUniqueID = null; // optional
208   
209    /**
210    * Returns the Issuer Unique ID optional field of the AC. Can be null, if it
211    * is missing in the AC.
212    */
 
213  0 toggle public BIT_STRING getIssuerUniqueID(){
214  0 return issuerUniqueID;
215    }
216   
217    /**
218    * Sets the Issuer Unique ID optional field of the AC. Can be null, if it is
219    * missing in the AC.
220    */
 
221  0 toggle public void setIssuerUniqueID(BIT_STRING issuerUniqueID){
222  0 this.issuerUniqueID=issuerUniqueID;
223    }
224   
225    protected Extensions extensions = null; // optional
226   
227    /**
228    * Returns the optional set of Extensions of the AC. Can be null, if no
229    * extensions are present in the AC.
230    */
 
231  1779 toggle public Extensions getExtensions(){
232  1779 return extensions;
233    }
234   
235    /**
236    * Sets the optional set of Extensions of the AC. Can be null, if no
237    * extensions are present in the AC.
238    */
 
239  0 toggle public void setExtensions(Extensions extensions){
240  0 this.extensions=extensions;
241    }
242   
 
243  2999 toggle protected AttributeCertificateInfo() {
244    }
245   
246    /**
247    * This constructor builds a AttributeCertificateInfo from a given ASN1Object.
248    *
249    * @param ao - the ASN1Object of the AttributeCertificateInfo
250    */
 
251  2979 toggle public AttributeCertificateInfo(ASN1Object ao) throws CodingException{
252  2979 decode(ao);
253    }
254   
255    /**
256    * This method copies the given AttributeCertificateInfo. All the multiple
257    * values (Attributes, Extensions) are copied, so modifying the original
258    * AttributeCertificateInfo does not affect the copy.
259    *
260    * @param aci - the AttributeCertifiacteInfo to copy
261    */
 
262  0 toggle public AttributeCertificateInfo(AttributeCertificateInfo aci) throws CodingException{
263  0 version = new AttCertVersion(aci.version);
264  0 holder = new Holder(aci.holder);
265  0 issuer = new AttCertIssuer(aci.issuer);
266  0 signature = new AlgorithmID(aci.signature.getAlgorithm(), aci.signature.getParameter());
267  0 serialNumber = new BigInteger(aci.serialNumber.toByteArray());
268  0 attrCertValidityPeriod = new AttCertValidityPeriod(aci.attrCertValidityPeriod);
269  0 attributes = new Vector(aci.attributes);
270    //for (Enumeration e=aci.attributes.elements(); e.hasMoreElements();){
271    // attributes.add(((Cloneable)e.nextElement()).clone());
272    //};
273   
274  0 issuerUniqueID = null;
275  0 if (aci.issuerUniqueID!=null){
276  0 issuerUniqueID = new BIT_STRING(aci.issuerUniqueID.getBinaryString());
277    }
278   
279  0 extensions = null;
280  0 if (aci.extensions!=null){
281  0 extensions = new Extensions(aci.extensions);
282    }
283    }
284   
285    /**
286    * This constructor builds an AttributeCertificateInfo from its components.
287    *
288    * @param version - the AttCertVersion with the version of this AC; the
289    * version represents the version of the AC spec: some features are not
290    * present in v1 ACs (e.g. AttCertIssuer can only be GeneralNames in
291    * v1 ACs); if null, the default version will be used (V1)
292    * @param holder - the Holder of the AC
293    * @param issuer - the AttCertIssuer with the issuer of this AC
294    * @param signature - the AlgorithmID of the signature algorithm
295    * @param serialNumber - the BigInteger containing the serial number of the AC
296    * @param attrCertValidityPeriod - the AttCertValidityPeriod with the validity
297    * period of this AC
298    * @param attributes - a Vector of attributes; each element must be an
299    * Attribute; cannot be null and should contain at least one element
300    * @param issuerUniqueID - the BIT_STRING of the issuer unique identifier;
301    * can be null, if it is not present
302    * @param extensions - the Extensions of this AC; can be null, if no
303    * extensions are present in this AC
304    */
 
305  20 toggle public AttributeCertificateInfo(AttCertVersion version,
306    Holder holder,
307    AttCertIssuer issuer,
308    AlgorithmID signature,
309    BigInteger serialNumber,
310    AttCertValidityPeriod attrCertValidityPeriod,
311    Vector attributes,
312    BIT_STRING issuerUniqueID,
313    Extensions extensions){
314  20 if (version == null){
315  0 version = new AttCertVersion(AttCertVersion.DEFAULT);
316    }
317  20 this.version = version;
318  20 this.holder = holder;
319  20 this.issuer = issuer;
320  20 this.signature = signature;
321  20 this.serialNumber = serialNumber;
322  20 this.attrCertValidityPeriod = attrCertValidityPeriod;
323  20 this.attributes = attributes;
324  20 this.issuerUniqueID = issuerUniqueID;
325  20 this.extensions = extensions;
326    }
327   
328    /**
329    * This method returns the ASN1Object of the AttributeCertificateInfo.
330    *
331    * @return SEQUENCE of the elements in the same order as they appear in the
332    * constructor prototype
333    */
 
334  42 toggle public ASN1Object toASN1Object() throws CodingException{
335  42 try{
336  42 ASN1Object seq = new SEQUENCE();
337   
338  42 if (attributes==null){
339  0 throw new CodingException("Attribute list is empty");
340    }else{
341    //Object [] atts = attributes.toArray();
342  84 for (int i=0; i<attributes.size(); i++){ // the last value of i will have no loop for j
343  42 String a = ((Attribute)attributes.elementAt(i)).type;
344  42 for (int j=i+1; j<attributes.size(); j++){
345  0 if (((Attribute)attributes.elementAt(j)).type.equals(a)){
346  0 throw new CodingException("Attributes of the same type encountered: ["+i+"]=["+j+"]="+a);
347    }
348    }
349  42 seq.addComponent(((issrg.ac.Attribute)attributes.elementAt(i)).toASN1Object());
350    }
351    }
352   
353  42 ASN1Object result = ASN.create(new ASN(ASN.SEQUENCE.getTag(), "AttributeCertificateInfo", ASN.UNIVERSAL));
354   
355  42 if (version.version!=version.DEFAULT){ // DEFAULT values should be not encoded
356  42 result.addComponent(version.toASN1Object());
357    }
358  42 result.addComponent(holder.toASN1Object());
359  42 result.addComponent(issuer.toASN1Object());
360  42 result.addComponent(signature.toASN1Object());
361  42 result.addComponent(new INTEGER(serialNumber));
362  42 result.addComponent(attrCertValidityPeriod.toASN1Object());
363  42 result.addComponent(seq);
364   
365  42 if (issuerUniqueID!=null){
366  0 result.addComponent(issuerUniqueID);
367    }
368   
369  42 if (extensions!=null){
370  42 result.addComponent(extensions.toASN1Object());
371    }
372   
373  42 return result;
374    }catch (InstantiationException ex){
375    //ex.printStackTrace();
376  0 throw new CodingException("Cannot encode due to tag instantiation exception");
377    }
378    }
379   
380    /**
381    * This method decodes a given ASN1Object and sets the contents of this
382    * object to the contents of this ASN1Object.
383    *
384    * @param ao - the ASN1Object with the contents of AttributeCertificateInfo;
385    * must be a SEQUENCE of elements in the same order as they appear in the
386    * constructor prototype
387    */
 
388  2979 toggle public void decode(ASN1Object ao) throws CodingException{
389  2979 ASN type = ao.getAsnType();
390  2979 if (type.getTag()!=ASN.SEQUENCE.getTag() || type.getTagClass()!=ASN.UNIVERSAL){
391  0 throw new CodingException("AttributeCertificateInfo SEQUENCE tag was expected");
392    }
393   
394  2979 int i=0;
395  2979 ASN1Object tag = ao.getComponentAt(i++);
396  2979 type = tag.getAsnType();
397   
398  2979 if (type.getTag()!=ASN.INTEGER.getTag() || type.getTagClass()!=ASN.UNIVERSAL){
399    // DEFAULT value encoded
400  739 version = new AttCertVersion((ASN1Object)null);
401    }else{
402  2240 version = new AttCertVersion(tag);
403   
404  2240 tag = ao.getComponentAt(i++);
405    }
406   
407  2979 holder = new Holder(tag);
408  2979 issuer = new AttCertIssuer(ao.getComponentAt(i++));
409  1859 signature = new AlgorithmID(ao.getComponentAt(i++));
410  1859 serialNumber = (BigInteger)((INTEGER)ao.getComponentAt(i++)).getValue();
411    // INTEGER is here just to cause ClassCastException in the case of mistake
412  1859 attrCertValidityPeriod = new AttCertValidityPeriod(ao.getComponentAt(i++));
413   
414  1859 attributes = new Vector();
415  1859 ASN1Object seq = ao.getComponentAt(i++);
416  3723 for(int j=seq.countComponents(); j-->0; ){ // picking elements in descending order so i won't need to call seq.countComponents() each time
417  1864 attributes.insertElementAt(new issrg.ac.Attribute(seq.getComponentAt(j)), 0);
418    }
419   
420  1859 if (attributes.size()==0){
421  0 throw new CodingException("Attribute set is empty");
422    }else{
423    //Object [] atts=attributes.toArray();
424  1864 for (int j=0; j<attributes.size()-1; j++){
425  5 String a = ((Attribute)attributes.elementAt(j)).type;
426  10 for (int k=j+1; k<attributes.size(); k++){
427  5 if (((Attribute)attributes.elementAt(k)).type.equals(a)){
428  0 throw new CodingException("Attributes of the same type encountered: ["+j+"]=["+k+"]="+a);
429    }
430    }
431    }
432    }
433  1859 issuerUniqueID = null;
434  1859 extensions = null;
435   
436  1859 if (ao.countComponents()>i){
437  1859 tag = ao.getComponentAt(i++);
438  1859 type = tag.getAsnType();
439   
440  1859 if (type.getTag()==ASN.BIT_STRING.getTag() && type.getTagClass()==ASN.UNIVERSAL){
441  739 issuerUniqueID = (BIT_STRING)tag;
442   
443  739 if (ao.countComponents()>i){
444  739 tag = ao.getComponentAt(i++);
445    }else{
446  0 tag = null;
447    }
448    }
449   
450  1859 if (tag!=null){
451  1859 extensions = new Extensions(tag);
452    }
453   
454  1859 if (ao.countComponents()>i){
455  0 throw new CodingException("Extra entries found in AttributeCertificateInfo tag: "+Integer.toString(ao.countComponents()));
456    }
457    }
458    }
459   
 
460  0 toggle public String toString(){
461  0 return toString("");
462    }
463   
 
464  4 toggle public String toString(String ident){
465  4 Enumeration e = attributes.elements();
466  4 String result =
467    "SEQUENCE { -- AttributeCertificateInfo --\n "+ident+
468    "version = "+version.toString(ident+" ")+",\n "+ident+
469    "holder = "+holder.toString(ident+" ")+",\n "+ident+
470    "issuer = "+issuer.toString(ident+" ")+",\n "+ident+
471    "signature = "+signature.toString()+",\n "+ident+
472    "serialNumber = "+serialNumber.toString()+",\n "+ident+
473    "attCertValidityPeriod = "+attrCertValidityPeriod.toString(ident+" ")+",\n "+ident+
474    "attributes = SEQUENCE { -- Sequence of Attributes -- ";
475   
476  4 if (e.hasMoreElements()){
477  4 result = result+"\n "+ident;
478  8 for(; e.hasMoreElements();){
479  4 result = result + " "+((Attribute)e.nextElement()).toString(ident+" ")+(e.hasMoreElements()?",":"")+"\n "+ident;
480    }
481    }
482   
483  4 return result+"},\n "+ident+
484  4 "issuerUniqueID = "+((issuerUniqueID!=null)?issuerUniqueID.toString():"[OPTIONAL; omitted]")+",\n "+ident+
485  4 "extensions = "+((extensions!=null)?extensions.toString(ident+" "):"[OPTIONAL; omitted]")+"\n"+ident+"}";
486    }
487   
 
488  0 toggle public Object clone(){
489  0 try{
490  0 return new AttributeCertificateInfo(this);
491    }catch (CodingException ex){
492  0 ex.printStackTrace();
493  0 return null;
494    }
495    }
496   
497    /**
498    * This method returns a DER encoding of the AttributeCertificateInfo, so you
499    * can compute the
500    * signature value for it.
501    *
502    * @throws iaik.asn1.CodingException if an error occurs while encoding the
503    * construct
504    */
 
505  20 toggle public byte[] getEncoded() throws CodingException {
506  20 return iaik.asn1.DerCoder.encode(toASN1Object());
507    }
508    }
509