KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > cmp > InfoTypeAndValue


1 // CMP implementation copyright (c) 2003 NOVOSEC AG (http://www.novosec.com)
2
//
3
// Author: Maik Stohn
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
6
// software and associated documentation files (the "Software"), to deal in the Software
7
// without restriction, including without limitation the rights to use, copy, modify, merge,
8
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
9
// to whom the Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in all copies or
12
// substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
15
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19

20 package com.novosec.pkix.asn1.cmp;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23
24 import org.bouncycastle.asn1.ASN1EncodableVector;
25 import org.bouncycastle.asn1.ASN1Sequence;
26 import org.bouncycastle.asn1.ASN1TaggedObject;
27 import org.bouncycastle.asn1.DEREncodable;
28 import org.bouncycastle.asn1.DERObject;
29 import org.bouncycastle.asn1.DERObjectIdentifier;
30 import org.bouncycastle.asn1.DEROutputStream;
31 import org.bouncycastle.asn1.DERSequence;
32
33 /**
34  * ASN.1 structure DER En/DeCoder.
35  *
36  * <pre>
37  * InfoTypeAndValue ::= SEQUENCE {
38  * infoType OBJECT IDENTIFIER,
39  * infoValue ANY DEFINED BY infoType OPTIONAL }
40  * </pre>
41  */

42 public class InfoTypeAndValue implements DEREncodable
43 {
44     private DERObjectIdentifier infoType;
45     private DEREncodable infoValue;
46     
47     public static InfoTypeAndValue getInstance( ASN1TaggedObject obj, boolean explicit )
48     {
49       return getInstance(ASN1Sequence.getInstance(obj, explicit));
50     }
51     
52     public static InfoTypeAndValue getInstance( Object JavaDoc obj )
53     {
54       if (obj instanceof InfoTypeAndValue)
55       {
56         return (InfoTypeAndValue)obj;
57       }
58       else if (obj instanceof ASN1Sequence)
59       {
60         return new InfoTypeAndValue((ASN1Sequence)obj);
61       }
62
63       throw new IllegalArgumentException JavaDoc("unknown object in factory");
64     }
65
66     public InfoTypeAndValue( ASN1Sequence seq )
67     {
68       infoType = (DERObjectIdentifier)seq.getObjectAt(0);
69
70       if (seq.size() == 2)
71         infoValue = seq.getObjectAt(1);
72       else
73         infoValue = null;
74     }
75
76     public InfoTypeAndValue( DERObjectIdentifier infoType )
77     {
78       this.infoType = infoType;
79     }
80
81     public DERObjectIdentifier getInfoType()
82     {
83       return infoType;
84     }
85
86     public DEREncodable getInfoValue()
87     {
88       return infoValue;
89     }
90
91     public void setInfoValue( DEREncodable infoValue )
92     {
93       this.infoValue = infoValue;
94     }
95
96     public DERObject getDERObject()
97     {
98       ASN1EncodableVector v = new ASN1EncodableVector();
99
100       v.add(infoType);
101
102       if( infoValue != null )
103         v.add(infoValue);
104
105       return new DERSequence(v);
106     }
107
108     public boolean equals( Object JavaDoc o )
109     {
110         if ((o == null) || !(o instanceof InfoTypeAndValue))
111         {
112             return false;
113         }
114
115         InfoTypeAndValue other = (InfoTypeAndValue)o;
116
117         if (!this.getInfoType().equals(other.getInfoType()))
118         {
119             return false;
120         }
121
122         if (this.getInfoValue() == null && other.getInfoValue() == null)
123         {
124             return true;
125         }
126
127         if (this.getInfoValue() == null || other.getInfoValue() == null)
128         {
129             return false;
130         }
131
132         ByteArrayOutputStream JavaDoc b1Out = new ByteArrayOutputStream JavaDoc();
133         ByteArrayOutputStream JavaDoc b2Out = new ByteArrayOutputStream JavaDoc();
134         DEROutputStream d1Out = new DEROutputStream(b1Out);
135         DEROutputStream d2Out = new DEROutputStream(b2Out);
136
137         try
138         {
139             d1Out.writeObject(this.getInfoValue());
140             d2Out.writeObject(other.getInfoValue());
141
142             byte[] b1 = b1Out.toByteArray();
143             byte[] b2 = b2Out.toByteArray();
144
145             if (b1.length != b2.length)
146             {
147                 return false;
148             }
149
150             for (int i = 0; i != b1.length; i++)
151             {
152                 if (b1[i] != b2[i])
153                 {
154                     return false;
155                 }
156             }
157         }
158         catch (Exception JavaDoc e)
159         {
160             return false;
161         }
162
163         return true;
164     }
165     
166     public String JavaDoc toString()
167     {
168       String JavaDoc s = "InfoTypeAndValue: (" + getInfoType();
169       
170       if( getInfoValue() != null )
171         s += ", " + getInfoValue();
172         
173       s += ")";
174         
175       return s;
176     }
177 }
178
Popular Tags