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 1 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     *      }
038     * </pre>
039     *
040     */
041    public class V1TBSCertificateGenerator
042    {
043        DERTaggedObject         version = new DERTaggedObject(0, new DERInteger(0));
044    
045        DERInteger              serialNumber;
046        AlgorithmIdentifier     signature;
047        X509Name                issuer;
048        Time                    startDate, endDate;
049        X509Name                subject;
050        SubjectPublicKeyInfo    subjectPublicKeyInfo;
051    
052        public V1TBSCertificateGenerator()
053        {
054        }
055    
056        public void setSerialNumber(
057            DERInteger  serialNumber)
058        {
059            this.serialNumber = serialNumber;
060        }
061    
062        public void setSignature(
063            AlgorithmIdentifier    signature)
064        {
065            this.signature = signature;
066        }
067    
068        public void setIssuer(
069            X509Name    issuer)
070        {
071            this.issuer = issuer;
072        }
073    
074        public void setStartDate(
075            Time startDate)
076        {
077            this.startDate = startDate;
078        }
079    
080        public void setStartDate(
081            DERUTCTime startDate)
082        {
083            this.startDate = new Time(startDate);
084        }
085    
086        public void setEndDate(
087            Time endDate)
088        {
089            this.endDate = endDate;
090        }
091    
092        public void setEndDate(
093            DERUTCTime endDate)
094        {
095            this.endDate = new Time(endDate);
096        }
097    
098        public void setSubject(
099            X509Name    subject)
100        {
101            this.subject = subject;
102        }
103    
104        public void setSubjectPublicKeyInfo(
105            SubjectPublicKeyInfo    pubKeyInfo)
106        {
107            this.subjectPublicKeyInfo = pubKeyInfo;
108        }
109    
110        public TBSCertificateStructure generateTBSCertificate()
111        {
112            if ((serialNumber == null) || (signature == null)
113                || (issuer == null) || (startDate == null) || (endDate == null)
114                || (subject == null) || (subjectPublicKeyInfo == null))
115            {
116                throw new IllegalStateException("not all mandatory fields set in V1 TBScertificate generator");
117            }
118    
119            ASN1EncodableVector  seq = new ASN1EncodableVector();
120    
121            // seq.add(version); - not required as default value.
122            seq.add(serialNumber);
123            seq.add(signature);
124            seq.add(issuer);
125    
126            //
127            // before and after dates
128            //
129            ASN1EncodableVector  validity = new ASN1EncodableVector();
130    
131            validity.add(startDate);
132            validity.add(endDate);
133    
134            seq.add(new DERSequence(validity));
135    
136            seq.add(subject);
137    
138            seq.add(subjectPublicKeyInfo);
139    
140            return new TBSCertificateStructure(new DERSequence(seq));
141        }
142    }