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.pkcs;
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.ASN1Set;
025    import org.apache.geronimo.util.asn1.DERInteger;
026    import org.apache.geronimo.util.asn1.DERObject;
027    import org.apache.geronimo.util.asn1.DERSequence;
028    import org.apache.geronimo.util.asn1.DERTaggedObject;
029    import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
030    import org.apache.geronimo.util.asn1.x509.X509Name;
031    
032    /**
033     * PKCS10 CertificationRequestInfo object.
034     * <pre>
035     *  CertificationRequestInfo ::= SEQUENCE {
036     *   version             INTEGER { v1(0) } (v1,...),
037     *   subject             Name,
038     *   subjectPKInfo   SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
039     *   attributes          [0] Attributes{{ CRIAttributes }}
040     *  }
041     *
042     *  Attributes { ATTRIBUTE:IOSet } ::= SET OF Attribute{{ IOSet }}
043     *
044     *  Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
045     *    type    ATTRIBUTE.&id({IOSet}),
046     *    values  SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{\@type})
047     *  }
048     * </pre>
049     */
050    public class CertificationRequestInfo
051        extends ASN1Encodable
052    {
053        DERInteger              version = new DERInteger(0);
054        X509Name                subject;
055        SubjectPublicKeyInfo    subjectPKInfo;
056        ASN1Set                 attributes = null;
057    
058        public static CertificationRequestInfo getInstance(
059            Object  obj)
060        {
061            if (obj instanceof CertificationRequestInfo)
062            {
063                return (CertificationRequestInfo)obj;
064            }
065            else if (obj instanceof ASN1Sequence)
066            {
067                return new CertificationRequestInfo((ASN1Sequence)obj);
068            }
069    
070            throw new IllegalArgumentException("unknown object in factory");
071        }
072    
073        public CertificationRequestInfo(
074            X509Name                subject,
075            SubjectPublicKeyInfo    pkInfo,
076            ASN1Set                 attributes)
077        {
078            this.subject = subject;
079            this.subjectPKInfo = pkInfo;
080            this.attributes = attributes;
081    
082            if ((subject == null) || (version == null) || (subjectPKInfo == null))
083            {
084                throw new IllegalArgumentException("Not all mandatory fields set in CertificationRequestInfo generator.");
085            }
086        }
087    
088        public CertificationRequestInfo(
089            ASN1Sequence  seq)
090        {
091            version = (DERInteger)seq.getObjectAt(0);
092    
093            subject = X509Name.getInstance(seq.getObjectAt(1));
094            subjectPKInfo = SubjectPublicKeyInfo.getInstance(seq.getObjectAt(2));
095    
096            //
097            // some CertificationRequestInfo objects seem to treat this field
098            // as optional.
099            //
100            if (seq.size() > 3)
101            {
102                DERTaggedObject tagobj = (DERTaggedObject)seq.getObjectAt(3);
103                attributes = ASN1Set.getInstance(tagobj, false);
104            }
105    
106            if ((subject == null) || (version == null) || (subjectPKInfo == null))
107            {
108                throw new IllegalArgumentException("Not all mandatory fields set in CertificationRequestInfo generator.");
109            }
110        }
111    
112        public DERInteger getVersion()
113        {
114            return version;
115        }
116    
117        public X509Name getSubject()
118        {
119            return subject;
120        }
121    
122        public SubjectPublicKeyInfo getSubjectPublicKeyInfo()
123        {
124            return subjectPKInfo;
125        }
126    
127        public ASN1Set getAttributes()
128        {
129            return attributes;
130        }
131    
132        public DERObject toASN1Object()
133        {
134            ASN1EncodableVector  v = new ASN1EncodableVector();
135    
136            v.add(version);
137            v.add(subject);
138            v.add(subjectPKInfo);
139    
140            if (attributes != null)
141            {
142                v.add(new DERTaggedObject(false, 0, attributes));
143            }
144    
145            return new DERSequence(v);
146        }
147    }