001package org.apache.commons.ssl.org.bouncycastle.asn1.cmp;
002
003import java.math.BigInteger;
004
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer;
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.DERBitString;
012import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
013
014public class PKIStatusInfo
015    extends ASN1Object
016{
017    ASN1Integer      status;
018    PKIFreeText     statusString;
019    DERBitString    failInfo;
020
021    public static PKIStatusInfo getInstance(
022        ASN1TaggedObject obj,
023        boolean explicit)
024    {
025        return getInstance(ASN1Sequence.getInstance(obj, explicit));
026    }
027
028    public static PKIStatusInfo getInstance(
029        Object obj)
030    {
031        if (obj instanceof PKIStatusInfo)
032        {
033            return (PKIStatusInfo)obj;
034        }
035        else if (obj != null)
036        {
037            return new PKIStatusInfo(ASN1Sequence.getInstance(obj));
038        }
039
040        return null;
041    }
042
043    private PKIStatusInfo(
044        ASN1Sequence seq)
045    {
046        this.status = ASN1Integer.getInstance(seq.getObjectAt(0));
047
048        this.statusString = null;
049        this.failInfo = null;
050
051        if (seq.size() > 2)
052        {
053            this.statusString = PKIFreeText.getInstance(seq.getObjectAt(1));
054            this.failInfo = DERBitString.getInstance(seq.getObjectAt(2));
055        }
056        else if (seq.size() > 1)
057        {
058            Object obj = seq.getObjectAt(1); 
059            if (obj instanceof DERBitString)
060            {
061                this.failInfo = DERBitString.getInstance(obj);
062            }
063            else
064            {
065                this.statusString = PKIFreeText.getInstance(obj);
066            }
067        }
068    }
069
070    /**
071     * @param status
072     */
073    public PKIStatusInfo(PKIStatus status)
074    {
075        this.status = ASN1Integer.getInstance(status.toASN1Primitive());
076    }
077
078    /**
079     *
080     * @param status
081     * @param statusString
082     */
083    public PKIStatusInfo(
084        PKIStatus   status,
085        PKIFreeText statusString)
086    {
087        this.status = ASN1Integer.getInstance(status.toASN1Primitive());
088        this.statusString = statusString;
089    }
090
091    public PKIStatusInfo(
092        PKIStatus      status,
093        PKIFreeText    statusString,
094        PKIFailureInfo failInfo)
095    {
096        this.status = ASN1Integer.getInstance(status.toASN1Primitive());
097        this.statusString = statusString;
098        this.failInfo = failInfo;
099    }
100    
101    public BigInteger getStatus()
102    {
103        return status.getValue();
104    }
105
106    public PKIFreeText getStatusString()
107    {
108        return statusString;
109    }
110
111    public DERBitString getFailInfo()
112    {
113        return failInfo;
114    }
115
116    /**
117     * <pre>
118     * PKIStatusInfo ::= SEQUENCE {
119     *     status        PKIStatus,                (INTEGER)
120     *     statusString  PKIFreeText     OPTIONAL,
121     *     failInfo      PKIFailureInfo  OPTIONAL  (BIT STRING)
122     * }
123     *
124     * PKIStatus:
125     *   granted                (0), -- you got exactly what you asked for
126     *   grantedWithMods        (1), -- you got something like what you asked for
127     *   rejection              (2), -- you don't get it, more information elsewhere in the message
128     *   waiting                (3), -- the request body part has not yet been processed, expect to hear more later
129     *   revocationWarning      (4), -- this message contains a warning that a revocation is imminent
130     *   revocationNotification (5), -- notification that a revocation has occurred
131     *   keyUpdateWarning       (6)  -- update already done for the oldCertId specified in CertReqMsg
132     *
133     * PKIFailureInfo:
134     *   badAlg           (0), -- unrecognized or unsupported Algorithm Identifier
135     *   badMessageCheck  (1), -- integrity check failed (e.g., signature did not verify)
136     *   badRequest       (2), -- transaction not permitted or supported
137     *   badTime          (3), -- messageTime was not sufficiently close to the system time, as defined by local policy
138     *   badCertId        (4), -- no certificate could be found matching the provided criteria
139     *   badDataFormat    (5), -- the data submitted has the wrong format
140     *   wrongAuthority   (6), -- the authority indicated in the request is different from the one creating the response token
141     *   incorrectData    (7), -- the requester's data is incorrect (for notary services)
142     *   missingTimeStamp (8), -- when the timestamp is missing but should be there (by policy)
143     *   badPOP           (9)  -- the proof-of-possession failed
144     *
145     * </pre>
146     */
147    public ASN1Primitive toASN1Primitive()
148    {
149        ASN1EncodableVector  v = new ASN1EncodableVector();
150
151        v.add(status);
152
153        if (statusString != null)
154        {
155            v.add(statusString);
156        }
157
158        if (failInfo!= null)
159        {
160            v.add(failInfo);
161        }
162
163        return new DERSequence(v);
164    }
165}