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.ASN1Object;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1OctetString;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
009import org.apache.commons.ssl.org.bouncycastle.asn1.DEROctetString;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
011import org.apache.commons.ssl.org.bouncycastle.asn1.x509.AlgorithmIdentifier;
012
013public class Challenge
014    extends ASN1Object
015{
016    private AlgorithmIdentifier owf;
017    private ASN1OctetString witness;
018    private ASN1OctetString challenge;
019
020    private Challenge(ASN1Sequence seq)
021    {
022        int index = 0;
023
024        if (seq.size() == 3)
025        {
026            owf = AlgorithmIdentifier.getInstance(seq.getObjectAt(index++));
027        }
028
029        witness = ASN1OctetString.getInstance(seq.getObjectAt(index++));
030        challenge = ASN1OctetString.getInstance(seq.getObjectAt(index));
031    }
032
033    public static Challenge getInstance(Object o)
034    {
035        if (o instanceof Challenge)
036        {
037            return (Challenge)o;
038        }
039
040        if (o != null)
041        {
042            return new Challenge(ASN1Sequence.getInstance(o));
043        }
044
045        return null;
046    }
047
048    public Challenge(byte[] witness, byte[] challenge)
049    {
050        this(null, witness, challenge);
051    }
052
053    public Challenge(AlgorithmIdentifier owf, byte[] witness, byte[] challenge)
054    {
055        this.owf = owf;
056        this.witness = new DEROctetString(witness);
057        this.challenge = new DEROctetString(challenge);
058    }
059
060    public AlgorithmIdentifier getOwf()
061    {
062        return owf;
063    }
064
065    public byte[] getWitness()
066    {
067        return witness.getOctets();
068    }
069
070    public byte[] getChallenge()
071    {
072        return challenge.getOctets();
073    }
074
075    /**
076     * <pre>
077     * Challenge ::= SEQUENCE {
078     *                 owf                 AlgorithmIdentifier  OPTIONAL,
079     *
080     *                 -- MUST be present in the first Challenge; MAY be omitted in
081     *                 -- any subsequent Challenge in POPODecKeyChallContent (if
082     *                 -- omitted, then the owf used in the immediately preceding
083     *                 -- Challenge is to be used).
084     *
085     *                 witness             OCTET STRING,
086     *                 -- the result of applying the one-way function (owf) to a
087     *                 -- randomly-generated INTEGER, A.  [Note that a different
088     *                 -- INTEGER MUST be used for each Challenge.]
089     *                 challenge           OCTET STRING
090     *                 -- the encryption (under the public key for which the cert.
091     *                 -- request is being made) of Rand, where Rand is specified as
092     *                 --   Rand ::= SEQUENCE {
093     *                 --      int      INTEGER,
094     *                 --       - the randomly-generated INTEGER A (above)
095     *                 --      sender   GeneralName
096     *                 --       - the sender's name (as included in PKIHeader)
097     *                 --   }
098     *      }
099     * </pre>
100     * @return a basic ASN.1 object representation.
101     */
102    public ASN1Primitive toASN1Primitive()
103    {
104        ASN1EncodableVector v = new ASN1EncodableVector();
105
106        addOptional(v, owf);
107        v.add(witness);
108        v.add(challenge);
109
110        return new DERSequence(v);
111    }
112
113    private void addOptional(ASN1EncodableVector v, ASN1Encodable obj)
114    {
115        if (obj != null)
116        {
117            v.add(obj);
118        }
119    }
120}