001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.util.asn1.x509;
020    
021    import org.apache.geronimo.util.asn1.ASN1Encodable;
022    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
023    import org.apache.geronimo.util.asn1.ASN1Sequence;
024    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
025    import org.apache.geronimo.util.asn1.DERBitString;
026    import org.apache.geronimo.util.asn1.DERInteger;
027    import org.apache.geronimo.util.asn1.DERObject;
028    import org.apache.geronimo.util.asn1.DERSequence;
029    
030    public class AttributeCertificateInfo
031        extends ASN1Encodable
032    {
033        private DERInteger              version;
034        private Holder                  holder;
035        private AttCertIssuer           issuer;
036        private AlgorithmIdentifier     signature;
037        private DERInteger              serialNumber;
038        private AttCertValidityPeriod   attrCertValidityPeriod;
039        private ASN1Sequence            attributes;
040        private DERBitString            issuerUniqueID;
041        private X509Extensions          extensions;
042    
043        public static AttributeCertificateInfo getInstance(
044            ASN1TaggedObject obj,
045            boolean          explicit)
046        {
047            return getInstance(ASN1Sequence.getInstance(obj, explicit));
048        }
049    
050        public static AttributeCertificateInfo getInstance(
051            Object  obj)
052        {
053            if (obj instanceof AttributeCertificateInfo)
054            {
055                return (AttributeCertificateInfo)obj;
056            }
057            else if (obj instanceof ASN1Sequence)
058            {
059                return new AttributeCertificateInfo((ASN1Sequence)obj);
060            }
061    
062            throw new IllegalArgumentException("unknown object in factory");
063        }
064    
065        public AttributeCertificateInfo(
066            ASN1Sequence   seq)
067        {
068            this.version = DERInteger.getInstance(seq.getObjectAt(0));
069            this.holder = Holder.getInstance(seq.getObjectAt(1));
070            this.issuer = AttCertIssuer.getInstance(seq.getObjectAt(2));
071            this.signature = AlgorithmIdentifier.getInstance(seq.getObjectAt(3));
072            this.serialNumber = DERInteger.getInstance(seq.getObjectAt(4));
073            this.attrCertValidityPeriod = AttCertValidityPeriod.getInstance(seq.getObjectAt(5));
074            this.attributes = ASN1Sequence.getInstance(seq.getObjectAt(6));
075    
076            for (int i = 7; i < seq.size(); i++)
077            {
078                ASN1Encodable    obj = (ASN1Encodable)seq.getObjectAt(i);
079    
080                if (obj instanceof DERBitString)
081                {
082                    this.issuerUniqueID = DERBitString.getInstance(seq.getObjectAt(i));
083                }
084                else if (obj instanceof ASN1Sequence || obj instanceof X509Extensions)
085                {
086                    this.extensions = X509Extensions.getInstance(seq.getObjectAt(i));
087                }
088            }
089        }
090    
091        public DERInteger getVersion()
092        {
093            return version;
094        }
095    
096        public Holder getHolder()
097        {
098            return holder;
099        }
100    
101        public AttCertIssuer getIssuer()
102        {
103            return issuer;
104        }
105    
106        public AlgorithmIdentifier getSignature()
107        {
108            return signature;
109        }
110    
111        public DERInteger getSerialNumber()
112        {
113            return serialNumber;
114        }
115    
116        public AttCertValidityPeriod getAttrCertValidityPeriod()
117        {
118            return attrCertValidityPeriod;
119        }
120    
121        public ASN1Sequence getAttributes()
122        {
123            return attributes;
124        }
125    
126        public DERBitString getIssuerUniqueID()
127        {
128            return issuerUniqueID;
129        }
130    
131        public X509Extensions getExtensions()
132        {
133            return extensions;
134        }
135    
136        /**
137         * Produce an object suitable for an ASN1OutputStream.
138         * <pre>
139         *  AttributeCertificateInfo ::= SEQUENCE {
140         *       version              AttCertVersion -- version is v2,
141         *       holder               Holder,
142         *       issuer               AttCertIssuer,
143         *       signature            AlgorithmIdentifier,
144         *       serialNumber         CertificateSerialNumber,
145         *       attrCertValidityPeriod   AttCertValidityPeriod,
146         *       attributes           SEQUENCE OF Attribute,
147         *       issuerUniqueID       UniqueIdentifier OPTIONAL,
148         *       extensions           Extensions OPTIONAL
149         *  }
150         *
151         *  AttCertVersion ::= INTEGER { v2(1) }
152         * </pre>
153         */
154        public DERObject toASN1Object()
155        {
156            ASN1EncodableVector  v = new ASN1EncodableVector();
157    
158            v.add(version);
159            v.add(holder);
160            v.add(issuer);
161            v.add(signature);
162            v.add(serialNumber);
163            v.add(attrCertValidityPeriod);
164            v.add(attributes);
165    
166            if (issuerUniqueID != null)
167            {
168                v.add(issuerUniqueID);
169            }
170    
171            if (extensions != null)
172            {
173                v.add(extensions);
174            }
175    
176            return new DERSequence(v);
177        }
178    }