1    package org.bouncycastle.asn1.x509;
2    
3    import java.io.*;
4    import java.util.Enumeration;
5    
6    import org.bouncycastle.asn1.*;
7    
8    public class AlgorithmIdentifier
9        implements DEREncodable
10   {
11       private DERObjectIdentifier objectId;
12       private DEREncodable        parameters;
13       private boolean             parametersDefined = false;
14           
15       public static AlgorithmIdentifier getInstance(
16           ASN1TaggedObject obj,
17           boolean          explicit)
18       {
19           return getInstance(ASN1Sequence.getInstance(obj, explicit));
20       }
21       
22       public static AlgorithmIdentifier getInstance(
23           Object  obj)
24       {
25           if (obj instanceof AlgorithmIdentifier)
26           {
27               return (AlgorithmIdentifier)obj;
28           }
29           
30           if (obj instanceof DERObjectIdentifier)
31           {
32               return new AlgorithmIdentifier((DERObjectIdentifier)obj);
33           }
34   
35           if (obj instanceof String)
36           {
37               return new AlgorithmIdentifier((String)obj);
38           }
39   
40           if (obj instanceof ASN1Sequence)
41           {
42               return new AlgorithmIdentifier((ASN1Sequence)obj);
43           }
44   
45           throw new IllegalArgumentException("unknown object in factory");
46       }
47   
48       public AlgorithmIdentifier(
49           DERObjectIdentifier     objectId)
50       {
51           this.objectId = objectId;
52       }
53   
54       public AlgorithmIdentifier(
55           String     objectId)
56       {
57           this.objectId = new DERObjectIdentifier(objectId);
58       }
59   
60       public AlgorithmIdentifier(
61           DERObjectIdentifier     objectId,
62           DEREncodable            parameters)
63       {
64           parametersDefined = true;
65           this.objectId = objectId;
66           this.parameters = parameters;
67       }
68   
69       public AlgorithmIdentifier(
70           ASN1Sequence   seq)
71       {
72           objectId = (DERObjectIdentifier)seq.getObjectAt(0);
73   
74           if (seq.size() == 2)
75           {
76               parametersDefined = true;
77               parameters = seq.getObjectAt(1);
78           }
79           else
80           {
81               parameters = null;
82           }
83       }
84   
85       public DERObjectIdentifier getObjectId()
86       {
87           return objectId;
88       }
89   
90       public DEREncodable getParameters()
91       {
92           return parameters;
93       }
94   
95       /**
96        * <pre>
97        *      AlgorithmIdentifier ::= SEQUENCE {
98        *                            algorithm OBJECT IDENTIFIER,
99        *                            parameters ANY DEFINED BY algorithm OPTIONAL }
100       * </pre>
101       */
102      public DERObject getDERObject()
103      {
104          DERConstructedSequence  seq = new DERConstructedSequence();
105  
106          seq.addObject(objectId);
107  
108          if (parametersDefined)
109          {
110              seq.addObject(parameters);
111          }
112  
113          return seq;
114      }
115  
116      public boolean equals(
117          Object  o)
118      {
119          if ((o == null) || !(o instanceof AlgorithmIdentifier))
120          {
121              return false;
122          }
123  
124          AlgorithmIdentifier other = (AlgorithmIdentifier)o;
125  
126          if (!this.getObjectId().equals(other.getObjectId()))
127          {
128              return false;
129          }
130  
131          if (this.getParameters() == null && other.getParameters() == null)
132          {
133              return true;
134          }
135  
136          if (this.getParameters() == null || other.getParameters() == null)
137          {
138              return false;
139          }
140  
141          ByteArrayOutputStream   b1Out = new ByteArrayOutputStream();
142          ByteArrayOutputStream   b2Out = new ByteArrayOutputStream();
143          DEROutputStream         d1Out = new DEROutputStream(b1Out);
144          DEROutputStream         d2Out = new DEROutputStream(b2Out);
145  
146          try
147          {
148              d1Out.writeObject(this.getParameters());
149              d2Out.writeObject(other.getParameters());
150  
151              byte[]  b1 = b1Out.toByteArray();
152              byte[]  b2 = b2Out.toByteArray();
153  
154              if (b1.length != b2.length)
155              {
156                  return false;
157              }
158  
159              for (int i = 0; i != b1.length; i++)
160              {
161                  if (b1[i] != b2[i])
162                  {
163                      return false;
164                  }
165              }
166          }
167          catch (Exception e)
168          {
169              return false;
170          }
171  
172          return true;
173      }
174  }
175