001package org.apache.commons.ssl.org.bouncycastle.asn1.cmp;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1OctetString;
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.DERSequence;
011
012public class CertResponse
013    extends ASN1Object
014{
015    private ASN1Integer certReqId;
016    private PKIStatusInfo status;
017    private CertifiedKeyPair certifiedKeyPair;
018    private ASN1OctetString rspInfo;
019
020    private CertResponse(ASN1Sequence seq)
021    {
022        certReqId = ASN1Integer.getInstance(seq.getObjectAt(0));
023        status = PKIStatusInfo.getInstance(seq.getObjectAt(1));
024
025        if (seq.size() >= 3)
026        {
027            if (seq.size() == 3)
028            {
029                ASN1Encodable o = seq.getObjectAt(2);
030                if (o instanceof ASN1OctetString)
031                {
032                    rspInfo = ASN1OctetString.getInstance(o);
033                }
034                else
035                {
036                    certifiedKeyPair = CertifiedKeyPair.getInstance(o);
037                }
038            }
039            else
040            {
041                certifiedKeyPair = CertifiedKeyPair.getInstance(seq.getObjectAt(2));
042                rspInfo = ASN1OctetString.getInstance(seq.getObjectAt(3));
043            }
044        }
045    }
046
047    public static CertResponse getInstance(Object o)
048    {
049        if (o instanceof CertResponse)
050        {
051            return (CertResponse)o;
052        }
053
054        if (o != null)
055        {
056            return new CertResponse(ASN1Sequence.getInstance(o));
057        }
058
059        return null;
060    }
061
062    public CertResponse(
063        ASN1Integer certReqId,
064        PKIStatusInfo status)
065    {
066        this(certReqId, status, null, null);
067    }
068
069    public CertResponse(
070        ASN1Integer certReqId,
071        PKIStatusInfo status,
072        CertifiedKeyPair certifiedKeyPair,
073        ASN1OctetString rspInfo)
074    {
075        if (certReqId == null)
076        {
077            throw new IllegalArgumentException("'certReqId' cannot be null");
078        }
079        if (status == null)
080        {
081            throw new IllegalArgumentException("'status' cannot be null");
082        }
083        this.certReqId = certReqId;
084        this.status = status;
085        this.certifiedKeyPair = certifiedKeyPair;
086        this.rspInfo = rspInfo;
087    }
088
089    public ASN1Integer getCertReqId()
090    {
091        return certReqId;
092    }
093
094    public PKIStatusInfo getStatus()
095    {
096        return status;
097    }
098
099    public CertifiedKeyPair getCertifiedKeyPair()
100    {
101        return certifiedKeyPair;
102    }
103
104    /**
105     * <pre>
106     * CertResponse ::= SEQUENCE {
107     *                            certReqId           INTEGER,
108     *                            -- to match this response with corresponding request (a value
109     *                            -- of -1 is to be used if certReqId is not specified in the
110     *                            -- corresponding request)
111     *                            status              PKIStatusInfo,
112     *                            certifiedKeyPair    CertifiedKeyPair    OPTIONAL,
113     *                            rspInfo             OCTET STRING        OPTIONAL
114     *                            -- analogous to the id-regInfo-utf8Pairs string defined
115     *                            -- for regInfo in CertReqMsg [CRMF]
116     *             }
117     * </pre> 
118     * @return a basic ASN.1 object representation.
119     */
120    public ASN1Primitive toASN1Primitive()
121    {
122        ASN1EncodableVector v = new ASN1EncodableVector();
123
124        v.add(certReqId);
125        v.add(status);
126
127        if (certifiedKeyPair != null)
128        {
129            v.add(certifiedKeyPair);
130        }
131
132        if (rspInfo != null)
133        {
134            v.add(rspInfo);
135        }
136        
137        return new DERSequence(v);
138    }
139}