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.ASN1EncodableVector;
021    import org.apache.geronimo.util.asn1.DERInteger;
022    import org.apache.geronimo.util.asn1.DERSequence;
023    import org.apache.geronimo.util.asn1.DERTaggedObject;
024    import org.apache.geronimo.util.asn1.DERUTCTime;
025    
026    /**
027     * Generator for Version 3 TBSCertificateStructures.
028     * <pre>
029     * TBSCertificate ::= SEQUENCE {
030     *      version          [ 0 ]  Version DEFAULT v1(0),
031     *      serialNumber            CertificateSerialNumber,
032     *      signature               AlgorithmIdentifier,
033     *      issuer                  Name,
034     *      validity                Validity,
035     *      subject                 Name,
036     *      subjectPublicKeyInfo    SubjectPublicKeyInfo,
037     *      issuerUniqueID    [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
038     *      subjectUniqueID   [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
039     *      extensions        [ 3 ] Extensions OPTIONAL
040     *      }
041     * </pre>
042     *
043     */
044    public class V3TBSCertificateGenerator
045    {
046        DERTaggedObject         version = new DERTaggedObject(0, new DERInteger(2));
047    
048        DERInteger              serialNumber;
049        AlgorithmIdentifier     signature;
050        X509Name                issuer;
051        Time                    startDate, endDate;
052        X509Name                subject;
053        SubjectPublicKeyInfo    subjectPublicKeyInfo;
054        X509Extensions          extensions;
055    
056        public V3TBSCertificateGenerator()
057        {
058        }
059    
060        public void setSerialNumber(
061            DERInteger  serialNumber)
062        {
063            this.serialNumber = serialNumber;
064        }
065    
066        public void setSignature(
067            AlgorithmIdentifier    signature)
068        {
069            this.signature = signature;
070        }
071    
072        public void setIssuer(
073            X509Name    issuer)
074        {
075            this.issuer = issuer;
076        }
077    
078        public void setStartDate(
079            DERUTCTime startDate)
080        {
081            this.startDate = new Time(startDate);
082        }
083    
084        public void setStartDate(
085            Time startDate)
086        {
087            this.startDate = startDate;
088        }
089    
090        public void setEndDate(
091            DERUTCTime endDate)
092        {
093            this.endDate = new Time(endDate);
094        }
095    
096        public void setEndDate(
097            Time endDate)
098        {
099            this.endDate = endDate;
100        }
101    
102        public void setSubject(
103            X509Name    subject)
104        {
105            this.subject = subject;
106        }
107    
108        public void setSubjectPublicKeyInfo(
109            SubjectPublicKeyInfo    pubKeyInfo)
110        {
111            this.subjectPublicKeyInfo = pubKeyInfo;
112        }
113    
114        public void setExtensions(
115            X509Extensions    extensions)
116        {
117            this.extensions = extensions;
118        }
119    
120        public TBSCertificateStructure generateTBSCertificate()
121        {
122            if ((serialNumber == null) || (signature == null)
123                || (issuer == null) || (startDate == null) || (endDate == null)
124                || (subject == null) || (subjectPublicKeyInfo == null))
125            {
126                throw new IllegalStateException("not all mandatory fields set in V3 TBScertificate generator");
127            }
128    
129            ASN1EncodableVector  v = new ASN1EncodableVector();
130    
131            v.add(version);
132            v.add(serialNumber);
133            v.add(signature);
134            v.add(issuer);
135    
136            //
137            // before and after dates
138            //
139            ASN1EncodableVector  validity = new ASN1EncodableVector();
140    
141            validity.add(startDate);
142            validity.add(endDate);
143    
144            v.add(new DERSequence(validity));
145    
146            v.add(subject);
147    
148            v.add(subjectPublicKeyInfo);
149    
150            if (extensions != null)
151            {
152                v.add(new DERTaggedObject(3, extensions));
153            }
154    
155            return new TBSCertificateStructure(new DERSequence(v));
156        }
157    }