001package org.apache.commons.ssl.org.bouncycastle.asn1.cmp;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Choice;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
008import org.apache.commons.ssl.org.bouncycastle.asn1.DERTaggedObject;
009import org.apache.commons.ssl.org.bouncycastle.asn1.crmf.CertReqMessages;
010import org.apache.commons.ssl.org.bouncycastle.asn1.pkcs.CertificationRequest;
011
012public class PKIBody
013    extends ASN1Object
014    implements ASN1Choice
015{
016    public static final int TYPE_INIT_REQ = 0;
017    public static final int TYPE_INIT_REP = 1;
018    public static final int TYPE_CERT_REQ = 2;
019    public static final int TYPE_CERT_REP = 3;
020    public static final int TYPE_P10_CERT_REQ = 4;
021    public static final int TYPE_POPO_CHALL = 5;
022    public static final int TYPE_POPO_REP = 6;
023    public static final int TYPE_KEY_UPDATE_REQ = 7;
024    public static final int TYPE_KEY_UPDATE_REP = 8;
025    public static final int TYPE_KEY_RECOVERY_REQ = 9;
026    public static final int TYPE_KEY_RECOVERY_REP = 10;
027    public static final int TYPE_REVOCATION_REQ = 11;
028    public static final int TYPE_REVOCATION_REP = 12;
029    public static final int TYPE_CROSS_CERT_REQ = 13;
030    public static final int TYPE_CROSS_CERT_REP = 14;
031    public static final int TYPE_CA_KEY_UPDATE_ANN = 15;
032    public static final int TYPE_CERT_ANN = 16;
033    public static final int TYPE_REVOCATION_ANN = 17;
034    public static final int TYPE_CRL_ANN = 18;
035    public static final int TYPE_CONFIRM = 19;
036    public static final int TYPE_NESTED = 20;
037    public static final int TYPE_GEN_MSG = 21;
038    public static final int TYPE_GEN_REP = 22;
039    public static final int TYPE_ERROR = 23;
040    public static final int TYPE_CERT_CONFIRM = 24;
041    public static final int TYPE_POLL_REQ = 25;
042    public static final int TYPE_POLL_REP = 26;
043
044    private int tagNo;
045    private ASN1Encodable body;
046
047    public static PKIBody getInstance(Object o)
048    {
049        if (o == null || o instanceof PKIBody)
050        {
051            return (PKIBody)o;
052        }
053
054        if (o instanceof ASN1TaggedObject)
055        {
056            return new PKIBody((ASN1TaggedObject)o);
057        }
058
059        throw new IllegalArgumentException("Invalid object: " + o.getClass().getName());
060    }
061
062    private PKIBody(ASN1TaggedObject tagged)
063    {
064        tagNo = tagged.getTagNo();
065        body = getBodyForType(tagNo, tagged.getObject());
066    }
067
068    /**
069     * Creates a new PKIBody.
070     * @param type one of the TYPE_* constants
071     * @param content message content
072     */
073    public PKIBody(
074        int type,
075        ASN1Encodable content)
076    {
077        tagNo = type;
078        body = getBodyForType(type, content);
079    }
080
081    private static ASN1Encodable getBodyForType(
082        int type,
083        ASN1Encodable o)
084    {
085        switch (type)
086        {
087        case TYPE_INIT_REQ:
088            return CertReqMessages.getInstance(o);
089        case TYPE_INIT_REP:
090            return CertRepMessage.getInstance(o);
091        case TYPE_CERT_REQ:
092            return CertReqMessages.getInstance(o);
093        case TYPE_CERT_REP:
094            return CertRepMessage.getInstance(o);
095        case TYPE_P10_CERT_REQ:
096            return CertificationRequest.getInstance(o);
097        case TYPE_POPO_CHALL:
098            return POPODecKeyChallContent.getInstance(o);
099        case TYPE_POPO_REP:
100            return POPODecKeyRespContent.getInstance(o);
101        case TYPE_KEY_UPDATE_REQ:
102            return CertReqMessages.getInstance(o);
103        case TYPE_KEY_UPDATE_REP:
104            return CertRepMessage.getInstance(o);
105        case TYPE_KEY_RECOVERY_REQ:
106            return CertReqMessages.getInstance(o);
107        case TYPE_KEY_RECOVERY_REP:
108            return KeyRecRepContent.getInstance(o);
109        case TYPE_REVOCATION_REQ:
110            return RevReqContent.getInstance(o);
111        case TYPE_REVOCATION_REP:
112            return RevRepContent.getInstance(o);
113        case TYPE_CROSS_CERT_REQ:
114            return CertReqMessages.getInstance(o);
115        case TYPE_CROSS_CERT_REP:
116            return CertRepMessage.getInstance(o);
117        case TYPE_CA_KEY_UPDATE_ANN:
118            return CAKeyUpdAnnContent.getInstance(o);
119        case TYPE_CERT_ANN:
120            return CMPCertificate.getInstance(o);
121        case TYPE_REVOCATION_ANN:
122            return RevAnnContent.getInstance(o);
123        case TYPE_CRL_ANN:
124            return CRLAnnContent.getInstance(o);
125        case TYPE_CONFIRM:
126            return PKIConfirmContent.getInstance(o);
127        case TYPE_NESTED:
128            return PKIMessages.getInstance(o);
129        case TYPE_GEN_MSG:
130            return GenMsgContent.getInstance(o);
131        case TYPE_GEN_REP:
132            return GenRepContent.getInstance(o);
133        case TYPE_ERROR:
134            return ErrorMsgContent.getInstance(o);
135        case TYPE_CERT_CONFIRM:
136            return CertConfirmContent.getInstance(o);
137        case TYPE_POLL_REQ:
138            return PollReqContent.getInstance(o);
139        case TYPE_POLL_REP:
140            return PollRepContent.getInstance(o);
141        default:
142            throw new IllegalArgumentException("unknown tag number: " + type);
143        }
144    }
145
146    public int getType()
147    {
148        return tagNo;
149    }
150
151    public ASN1Encodable getContent()
152    {
153        return body;
154    }
155
156    /**
157     * <pre>
158     * PKIBody ::= CHOICE {       -- message-specific body elements
159     *        ir       [0]  CertReqMessages,        --Initialization Request
160     *        ip       [1]  CertRepMessage,         --Initialization Response
161     *        cr       [2]  CertReqMessages,        --Certification Request
162     *        cp       [3]  CertRepMessage,         --Certification Response
163     *        p10cr    [4]  CertificationRequest,   --imported from [PKCS10]
164     *        popdecc  [5]  POPODecKeyChallContent, --pop Challenge
165     *        popdecr  [6]  POPODecKeyRespContent,  --pop Response
166     *        kur      [7]  CertReqMessages,        --Key Update Request
167     *        kup      [8]  CertRepMessage,         --Key Update Response
168     *        krr      [9]  CertReqMessages,        --Key Recovery Request
169     *        krp      [10] KeyRecRepContent,       --Key Recovery Response
170     *        rr       [11] RevReqContent,          --Revocation Request
171     *        rp       [12] RevRepContent,          --Revocation Response
172     *        ccr      [13] CertReqMessages,        --Cross-Cert. Request
173     *        ccp      [14] CertRepMessage,         --Cross-Cert. Response
174     *        ckuann   [15] CAKeyUpdAnnContent,     --CA Key Update Ann.
175     *        cann     [16] CertAnnContent,         --Certificate Ann.
176     *        rann     [17] RevAnnContent,          --Revocation Ann.
177     *        crlann   [18] CRLAnnContent,          --CRL Announcement
178     *        pkiconf  [19] PKIConfirmContent,      --Confirmation
179     *        nested   [20] NestedMessageContent,   --Nested Message
180     *        genm     [21] GenMsgContent,          --General Message
181     *        genp     [22] GenRepContent,          --General Response
182     *        error    [23] ErrorMsgContent,        --Error Message
183     *        certConf [24] CertConfirmContent,     --Certificate confirm
184     *        pollReq  [25] PollReqContent,         --Polling request
185     *        pollRep  [26] PollRepContent          --Polling response
186     * }
187     * </pre>
188     * @return a basic ASN.1 object representation.
189     */
190    public ASN1Primitive toASN1Primitive()
191    {
192        return new DERTaggedObject(true, tagNo, body);
193    }
194}