001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.util.asn1.x509;
019    
020    import org.apache.geronimo.util.asn1.ASN1Encodable;
021    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
022    import org.apache.geronimo.util.asn1.ASN1Sequence;
023    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
024    import org.apache.geronimo.util.asn1.DEREncodable;
025    import org.apache.geronimo.util.asn1.DERObject;
026    import org.apache.geronimo.util.asn1.DERObjectIdentifier;
027    import org.apache.geronimo.util.asn1.DERSequence;
028    
029    public class AlgorithmIdentifier
030        extends ASN1Encodable
031    {
032        private DERObjectIdentifier objectId;
033        private DEREncodable        parameters;
034        private boolean             parametersDefined = false;
035    
036        public static AlgorithmIdentifier getInstance(
037            ASN1TaggedObject obj,
038            boolean          explicit)
039        {
040            return getInstance(ASN1Sequence.getInstance(obj, explicit));
041        }
042    
043        public static AlgorithmIdentifier getInstance(
044            Object  obj)
045        {
046            if (obj instanceof AlgorithmIdentifier)
047            {
048                return (AlgorithmIdentifier)obj;
049            }
050    
051            if (obj instanceof DERObjectIdentifier)
052            {
053                return new AlgorithmIdentifier((DERObjectIdentifier)obj);
054            }
055    
056            if (obj instanceof String)
057            {
058                return new AlgorithmIdentifier((String)obj);
059            }
060    
061            if (obj instanceof ASN1Sequence)
062            {
063                return new AlgorithmIdentifier((ASN1Sequence)obj);
064            }
065    
066            throw new IllegalArgumentException("unknown object in factory");
067        }
068    
069        public AlgorithmIdentifier(
070            DERObjectIdentifier     objectId)
071        {
072            this.objectId = objectId;
073        }
074    
075        public AlgorithmIdentifier(
076            String     objectId)
077        {
078            this.objectId = new DERObjectIdentifier(objectId);
079        }
080    
081        public AlgorithmIdentifier(
082            DERObjectIdentifier     objectId,
083            DEREncodable            parameters)
084        {
085            parametersDefined = true;
086            this.objectId = objectId;
087            this.parameters = parameters;
088        }
089    
090        public AlgorithmIdentifier(
091            ASN1Sequence   seq)
092        {
093            objectId = (DERObjectIdentifier)seq.getObjectAt(0);
094    
095            if (seq.size() == 2)
096            {
097                parametersDefined = true;
098                parameters = seq.getObjectAt(1);
099            }
100            else
101            {
102                parameters = null;
103            }
104        }
105    
106        public DERObjectIdentifier getObjectId()
107        {
108            return objectId;
109        }
110    
111        public DEREncodable getParameters()
112        {
113            return parameters;
114        }
115    
116        /**
117         * Produce an object suitable for an ASN1OutputStream.
118         * <pre>
119         *      AlgorithmIdentifier ::= SEQUENCE {
120         *                            algorithm OBJECT IDENTIFIER,
121         *                            parameters ANY DEFINED BY algorithm OPTIONAL }
122         * </pre>
123         */
124        public DERObject toASN1Object()
125        {
126            ASN1EncodableVector  v = new ASN1EncodableVector();
127    
128            v.add(objectId);
129    
130            if (parametersDefined)
131            {
132                v.add(parameters);
133            }
134    
135            return new DERSequence(v);
136        }
137    }