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.ASN1Sequence;
023    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
024    import org.apache.geronimo.util.asn1.DERBitString;
025    import org.apache.geronimo.util.asn1.DERInteger;
026    import org.apache.geronimo.util.asn1.DERObject;
027    import org.apache.geronimo.util.asn1.DERTaggedObject;
028    import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers;
029    
030    /**
031     * The TBSCertificate object.
032     * <pre>
033     * TBSCertificate ::= SEQUENCE {
034     *      version          [ 0 ]  Version DEFAULT v1(0),
035     *      serialNumber            CertificateSerialNumber,
036     *      signature               AlgorithmIdentifier,
037     *      issuer                  Name,
038     *      validity                Validity,
039     *      subject                 Name,
040     *      subjectPublicKeyInfo    SubjectPublicKeyInfo,
041     *      issuerUniqueID    [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
042     *      subjectUniqueID   [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
043     *      extensions        [ 3 ] Extensions OPTIONAL
044     *      }
045     * </pre>
046     * <p>
047     * Note: issuerUniqueID and subjectUniqueID are both deprecated by the IETF. This class
048     * will parse them, but you really shouldn't be creating new ones.
049     */
050    public class TBSCertificateStructure
051        extends ASN1Encodable
052        implements X509ObjectIdentifiers, PKCSObjectIdentifiers
053    {
054        ASN1Sequence            seq;
055    
056        DERInteger              version;
057        DERInteger              serialNumber;
058        AlgorithmIdentifier     signature;
059        X509Name                issuer;
060        Time                    startDate, endDate;
061        X509Name                subject;
062        SubjectPublicKeyInfo    subjectPublicKeyInfo;
063        DERBitString            issuerUniqueId;
064        DERBitString            subjectUniqueId;
065        X509Extensions          extensions;
066    
067        public static TBSCertificateStructure getInstance(
068            ASN1TaggedObject obj,
069            boolean          explicit)
070        {
071            return getInstance(ASN1Sequence.getInstance(obj, explicit));
072        }
073    
074        public static TBSCertificateStructure getInstance(
075            Object  obj)
076        {
077            if (obj instanceof TBSCertificateStructure)
078            {
079                return (TBSCertificateStructure)obj;
080            }
081            else if (obj instanceof ASN1Sequence)
082            {
083                return new TBSCertificateStructure((ASN1Sequence)obj);
084            }
085    
086            throw new IllegalArgumentException("unknown object in factory");
087        }
088    
089        public TBSCertificateStructure(
090            ASN1Sequence  seq)
091        {
092            int         seqStart = 0;
093    
094            this.seq = seq;
095    
096            //
097            // some certficates don't include a version number - we assume v1
098            //
099            if (seq.getObjectAt(0) instanceof DERTaggedObject)
100            {
101                version = DERInteger.getInstance(seq.getObjectAt(0));
102            }
103            else
104            {
105                seqStart = -1;          // field 0 is missing!
106                version = new DERInteger(0);
107            }
108    
109            serialNumber = DERInteger.getInstance(seq.getObjectAt(seqStart + 1));
110    
111            signature = AlgorithmIdentifier.getInstance(seq.getObjectAt(seqStart + 2));
112            issuer = X509Name.getInstance(seq.getObjectAt(seqStart + 3));
113    
114            //
115            // before and after dates
116            //
117            ASN1Sequence  dates = (ASN1Sequence)seq.getObjectAt(seqStart + 4);
118    
119            startDate = Time.getInstance(dates.getObjectAt(0));
120            endDate = Time.getInstance(dates.getObjectAt(1));
121    
122            subject = X509Name.getInstance(seq.getObjectAt(seqStart + 5));
123    
124            //
125            // public key info.
126            //
127            subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(seq.getObjectAt(seqStart + 6));
128    
129            for (int extras = seq.size() - (seqStart + 6) - 1; extras > 0; extras--)
130            {
131                DERTaggedObject extra = (DERTaggedObject)seq.getObjectAt(seqStart + 6 + extras);
132    
133                switch (extra.getTagNo())
134                {
135                case 1:
136                    issuerUniqueId = DERBitString.getInstance(extra, false);
137                    break;
138                case 2:
139                    subjectUniqueId = DERBitString.getInstance(extra, false);
140                    break;
141                case 3:
142                    extensions = X509Extensions.getInstance(extra);
143                }
144            }
145        }
146    
147        public int getVersion()
148        {
149            return version.getValue().intValue() + 1;
150        }
151    
152        public DERInteger getVersionNumber()
153        {
154            return version;
155        }
156    
157        public DERInteger getSerialNumber()
158        {
159            return serialNumber;
160        }
161    
162        public AlgorithmIdentifier getSignature()
163        {
164            return signature;
165        }
166    
167        public X509Name getIssuer()
168        {
169            return issuer;
170        }
171    
172        public Time getStartDate()
173        {
174            return startDate;
175        }
176    
177        public Time getEndDate()
178        {
179            return endDate;
180        }
181    
182        public X509Name getSubject()
183        {
184            return subject;
185        }
186    
187        public SubjectPublicKeyInfo getSubjectPublicKeyInfo()
188        {
189            return subjectPublicKeyInfo;
190        }
191    
192        public DERBitString getIssuerUniqueId()
193        {
194            return issuerUniqueId;
195        }
196    
197        public DERBitString getSubjectUniqueId()
198        {
199            return subjectUniqueId;
200        }
201    
202        public X509Extensions getExtensions()
203        {
204            return extensions;
205        }
206    
207        public DERObject toASN1Object()
208        {
209            return seq;
210        }
211    }