View Javadoc

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;
23  
24  import org.bouncycastle.asn1.ASN1Encodable;
25  import org.bouncycastle.asn1.ASN1EncodableVector;
26  import org.bouncycastle.asn1.ASN1ObjectIdentifier;
27  import org.bouncycastle.asn1.ASN1Primitive;
28  import org.bouncycastle.asn1.ASN1Sequence;
29  import org.bouncycastle.asn1.ASN1TaggedObject;
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 ASN1Encodable
43  {
44      private ASN1ObjectIdentifier type;
45      private ASN1Encodable        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  obj)
56      {
57          if (obj instanceof AttributeTypeAndValue)
58          {
59              return (AttributeTypeAndValue)obj;
60          }
61          
62          if (obj instanceof ASN1ObjectIdentifier)
63          {
64              return new AttributeTypeAndValue((ASN1ObjectIdentifier)obj);
65          }
66  
67          if (obj instanceof String)
68          {
69              return new AttributeTypeAndValue((String)obj);
70          }
71  
72          if (obj instanceof ASN1Sequence)
73          {
74              return new AttributeTypeAndValue((ASN1Sequence)obj);
75          }
76  
77          throw new IllegalArgumentException("unknown object in factory");
78      }
79  
80      public AttributeTypeAndValue(
81          ASN1ObjectIdentifier     type)
82      {
83          this.type = type;
84      }
85  
86      public AttributeTypeAndValue(
87          String     type)
88      {
89          this.type = new ASN1ObjectIdentifier(type);
90      }
91  
92      public AttributeTypeAndValue(ASN1ObjectIdentifier type, ASN1Encodable value )
93      {
94          this.type = type;
95          this.value = value;
96      }
97  
98      public AttributeTypeAndValue(ASN1Sequence seq)
99      {
100         type = (ASN1ObjectIdentifier)seq.getObjectAt(0);
101         value = seq.getObjectAt(1);
102     }
103 
104     public ASN1ObjectIdentifier getObjectId()
105     {
106         return type;
107     }
108 
109     public ASN1Encodable getParameters()
110     {
111         return value;
112     }
113 
114     public ASN1Primitive toASN1Primitive()
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 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   b1Out = new ByteArrayOutputStream();
149         ByteArrayOutputStream   b2Out = new ByteArrayOutputStream();
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 e)
175         {
176             return false;
177         }
178 
179         return true;
180     }
181 }