KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > crmf > AttributeTypeAndValue


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.crmf;
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  * AttributeTypeAndValue ::= SEQUENCE {
38  * type OBJECT IDENTIFIER,
39  * value ANY DEFINED BY type }
40  * </pre>
41  */

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