001package org.apache.commons.ssl.org.bouncycastle.asn1.crmf;
002
003import java.util.Enumeration;
004
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
010import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
011import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
012
013public class CertReqMsg
014    extends ASN1Object
015{
016    private CertRequest certReq;
017    private ProofOfPossession pop;
018    private ASN1Sequence regInfo;
019
020    private CertReqMsg(ASN1Sequence seq)
021    {
022        Enumeration en = seq.getObjects();
023
024        certReq = CertRequest.getInstance(en.nextElement());
025        while (en.hasMoreElements())
026        {
027            Object o = en.nextElement();
028
029            if (o instanceof ASN1TaggedObject || o instanceof ProofOfPossession)
030            {
031                pop = ProofOfPossession.getInstance(o);
032            }
033            else
034            {
035                regInfo = ASN1Sequence.getInstance(o);
036            }
037        }
038    }
039
040    public static CertReqMsg getInstance(Object o)
041    {
042        if (o instanceof CertReqMsg)
043        {
044            return (CertReqMsg)o;
045        }
046        else if (o != null)
047        {
048            return new CertReqMsg(ASN1Sequence.getInstance(o));
049        }
050
051        return null;
052    }
053
054    /**
055     * Creates a new CertReqMsg.
056     * @param certReq CertRequest
057     * @param pop may be null
058     * @param regInfo may be null
059     */
060    public CertReqMsg(
061        CertRequest certReq,
062        ProofOfPossession pop,
063        AttributeTypeAndValue[] regInfo)
064    {
065        if (certReq == null)
066        {
067            throw new IllegalArgumentException("'certReq' cannot be null");
068        }
069
070        this.certReq = certReq;
071        this.pop = pop;
072
073        if (regInfo != null)
074        {
075            this.regInfo = new DERSequence(regInfo);
076        }
077    }
078
079    public CertRequest getCertReq()
080    {
081        return certReq;
082    }
083
084
085    /**
086     * @deprecated use getPopo
087     */
088    public ProofOfPossession getPop()
089    {
090        return pop;
091    }
092
093
094    public ProofOfPossession getPopo()
095    {
096        return pop;
097    }
098
099    public AttributeTypeAndValue[] getRegInfo()
100    {
101        if (regInfo == null)
102        {
103            return null;
104        }
105
106        AttributeTypeAndValue[] results = new AttributeTypeAndValue[regInfo.size()];
107
108        for (int i = 0; i != results.length; i++)
109        {
110            results[i] = AttributeTypeAndValue.getInstance(regInfo.getObjectAt(i));
111        }
112
113        return results;
114    }
115
116    /**
117     * <pre>
118     * CertReqMsg ::= SEQUENCE {
119     *                    certReq   CertRequest,
120     *                    popo       ProofOfPossession  OPTIONAL,
121     *                    -- content depends upon key type
122     *                    regInfo   SEQUENCE SIZE(1..MAX) OF AttributeTypeAndValue OPTIONAL }
123     * </pre>
124     * @return a basic ASN.1 object representation.
125     */
126    public ASN1Primitive toASN1Primitive()
127    {
128        ASN1EncodableVector v = new ASN1EncodableVector();
129
130        v.add(certReq);
131
132        addOptional(v, pop);
133        addOptional(v, regInfo);
134
135        return new DERSequence(v);
136    }
137
138    private void addOptional(ASN1EncodableVector v, ASN1Encodable obj)
139    {
140        if (obj != null)
141        {
142            v.add(obj);
143        }
144    }
145}