View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.x509;
20  
21  import org.apache.geronimo.util.asn1.ASN1Encodable;
22  import org.apache.geronimo.util.asn1.ASN1EncodableVector;
23  import org.apache.geronimo.util.asn1.ASN1Sequence;
24  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
25  import org.apache.geronimo.util.asn1.DEREncodable;
26  import org.apache.geronimo.util.asn1.DERObject;
27  import org.apache.geronimo.util.asn1.DERObjectIdentifier;
28  import org.apache.geronimo.util.asn1.DERSequence;
29  
30  public class AlgorithmIdentifier
31      extends ASN1Encodable
32  {
33      private DERObjectIdentifier objectId;
34      private DEREncodable        parameters;
35      private boolean             parametersDefined = false;
36  
37      public static AlgorithmIdentifier getInstance(
38          ASN1TaggedObject obj,
39          boolean          explicit)
40      {
41          return getInstance(ASN1Sequence.getInstance(obj, explicit));
42      }
43  
44      public static AlgorithmIdentifier getInstance(
45          Object  obj)
46      {
47          if (obj instanceof AlgorithmIdentifier)
48          {
49              return (AlgorithmIdentifier)obj;
50          }
51  
52          if (obj instanceof DERObjectIdentifier)
53          {
54              return new AlgorithmIdentifier((DERObjectIdentifier)obj);
55          }
56  
57          if (obj instanceof String)
58          {
59              return new AlgorithmIdentifier((String)obj);
60          }
61  
62          if (obj instanceof ASN1Sequence)
63          {
64              return new AlgorithmIdentifier((ASN1Sequence)obj);
65          }
66  
67          throw new IllegalArgumentException("unknown object in factory");
68      }
69  
70      public AlgorithmIdentifier(
71          DERObjectIdentifier     objectId)
72      {
73          this.objectId = objectId;
74      }
75  
76      public AlgorithmIdentifier(
77          String     objectId)
78      {
79          this.objectId = new DERObjectIdentifier(objectId);
80      }
81  
82      public AlgorithmIdentifier(
83          DERObjectIdentifier     objectId,
84          DEREncodable            parameters)
85      {
86          parametersDefined = true;
87          this.objectId = objectId;
88          this.parameters = parameters;
89      }
90  
91      public AlgorithmIdentifier(
92          ASN1Sequence   seq)
93      {
94          objectId = (DERObjectIdentifier)seq.getObjectAt(0);
95  
96          if (seq.size() == 2)
97          {
98              parametersDefined = true;
99              parameters = seq.getObjectAt(1);
100         }
101         else
102         {
103             parameters = null;
104         }
105     }
106 
107     public DERObjectIdentifier getObjectId()
108     {
109         return objectId;
110     }
111 
112     public DEREncodable getParameters()
113     {
114         return parameters;
115     }
116 
117     /**
118      * Produce an object suitable for an ASN1OutputStream.
119      * <pre>
120      *      AlgorithmIdentifier ::= SEQUENCE {
121      *                            algorithm OBJECT IDENTIFIER,
122      *                            parameters ANY DEFINED BY algorithm OPTIONAL }
123      * </pre>
124      */
125     public DERObject toASN1Object()
126     {
127         ASN1EncodableVector  v = new ASN1EncodableVector();
128 
129         v.add(objectId);
130 
131         if (parametersDefined)
132         {
133             v.add(parameters);
134         }
135 
136         return new DERSequence(v);
137     }
138 }