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