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.DERObject;
025    import org.apache.geronimo.util.asn1.DERSequence;
026    import org.apache.geronimo.util.asn1.DERTaggedObject;
027    
028    public class V2Form
029        extends ASN1Encodable
030    {
031        GeneralNames        issuerName;
032        IssuerSerial        baseCertificateID;
033        ObjectDigestInfo    objectDigestInfo;
034    
035        public static V2Form getInstance(
036            ASN1TaggedObject obj,
037            boolean          explicit)
038        {
039            return getInstance(ASN1Sequence.getInstance(obj, explicit));
040        }
041    
042        public static V2Form getInstance(
043            Object  obj)
044        {
045            if (obj == null || obj instanceof V2Form)
046            {
047                return (V2Form)obj;
048            }
049            else if (obj instanceof ASN1Sequence)
050            {
051                return new V2Form((ASN1Sequence)obj);
052            }
053    
054            throw new IllegalArgumentException("unknown object in factory");
055        }
056    
057        public V2Form(
058            GeneralNames    issuerName)
059        {
060            this.issuerName = issuerName;
061        }
062    
063        public V2Form(
064            ASN1Sequence seq)
065        {
066            int    index = 0;
067    
068            if (!(seq.getObjectAt(0) instanceof ASN1TaggedObject))
069            {
070                index++;
071                this.issuerName = GeneralNames.getInstance(seq.getObjectAt(0));
072            }
073    
074            for (int i = index; i != seq.size(); i++)
075            {
076                ASN1TaggedObject o = (ASN1TaggedObject)seq.getObjectAt(i);
077                if (o.getTagNo() == 0)
078                {
079                    baseCertificateID = IssuerSerial.getInstance(o, false);
080                }
081                else if (o.getTagNo() == 1)
082                {
083                    objectDigestInfo = ObjectDigestInfo.getInstance(o, false);
084                }
085            }
086        }
087    
088        public GeneralNames getIssuerName()
089        {
090            return issuerName;
091        }
092    
093        public IssuerSerial getBaseCertificateID()
094        {
095            return baseCertificateID;
096        }
097    
098        public ObjectDigestInfo getObjectDigestInfo()
099        {
100            return objectDigestInfo;
101        }
102    
103        /**
104         * Produce an object suitable for an ASN1OutputStream.
105         * <pre>
106         *  V2Form ::= SEQUENCE {
107         *       issuerName            GeneralNames  OPTIONAL,
108         *       baseCertificateID     [0] IssuerSerial  OPTIONAL,
109         *       objectDigestInfo      [1] ObjectDigestInfo  OPTIONAL
110         *         -- issuerName MUST be present in this profile
111         *         -- baseCertificateID and objectDigestInfo MUST NOT
112         *         -- be present in this profile
113         *  }
114         * </pre>
115         */
116        public DERObject toASN1Object()
117        {
118            ASN1EncodableVector  v = new ASN1EncodableVector();
119    
120            if (issuerName != null)
121            {
122                v.add(issuerName);
123            }
124    
125            if (baseCertificateID != null)
126            {
127                v.add(new DERTaggedObject(false, 0, baseCertificateID));
128            }
129    
130            if (objectDigestInfo != null)
131            {
132                v.add(new DERTaggedObject(false, 1, objectDigestInfo));
133            }
134    
135            return new DERSequence(v);
136        }
137    }