001package org.apache.commons.ssl.org.bouncycastle.asn1.crmf;
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.ASN1Primitive;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
009import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DERTaggedObject;
011import org.apache.commons.ssl.org.bouncycastle.asn1.x509.GeneralName;
012import org.apache.commons.ssl.org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
013
014public class POPOSigningKeyInput
015    extends ASN1Object
016{
017    private GeneralName sender;
018    private PKMACValue publicKeyMAC;
019    private SubjectPublicKeyInfo publicKey;
020
021    private POPOSigningKeyInput(ASN1Sequence seq)
022    {
023        ASN1Encodable authInfo = (ASN1Encodable)seq.getObjectAt(0);
024
025        if (authInfo instanceof ASN1TaggedObject)
026        {
027            ASN1TaggedObject tagObj = (ASN1TaggedObject)authInfo;
028            if (tagObj.getTagNo() != 0)
029            {
030                throw new IllegalArgumentException(
031                    "Unknown authInfo tag: " + tagObj.getTagNo());
032            }
033            sender = GeneralName.getInstance(tagObj.getObject());
034        }
035        else
036        {
037            publicKeyMAC = PKMACValue.getInstance(authInfo);
038        }
039
040        publicKey = SubjectPublicKeyInfo.getInstance(seq.getObjectAt(1));
041    }
042
043    public static POPOSigningKeyInput getInstance(Object o)
044    {
045        if (o instanceof POPOSigningKeyInput)
046        {
047            return (POPOSigningKeyInput)o;
048        }
049
050        if (o != null)
051        {
052            return new POPOSigningKeyInput(ASN1Sequence.getInstance(o));
053        }
054
055        return null;
056    }
057
058    /**
059     *  Creates a new POPOSigningKeyInput with sender name as authInfo.
060     */
061    public POPOSigningKeyInput(
062        GeneralName sender,
063        SubjectPublicKeyInfo spki)
064    {
065        this.sender = sender;
066        this.publicKey = spki;
067    }
068
069    /**
070     * Creates a new POPOSigningKeyInput using password-based MAC.
071     */
072    public POPOSigningKeyInput(
073        PKMACValue pkmac,
074        SubjectPublicKeyInfo spki)
075    {
076        this.publicKeyMAC = pkmac;
077        this.publicKey = spki;
078    }
079
080    /**
081     * Returns the sender field, or null if authInfo is publicKeyMAC
082     */
083    public GeneralName getSender()
084    {
085        return sender;
086    }
087
088    /**
089     * Returns the publicKeyMAC field, or null if authInfo is sender
090     */
091    public PKMACValue getPublicKeyMAC()
092    {
093        return publicKeyMAC;
094    }
095
096    public SubjectPublicKeyInfo getPublicKey()
097    {
098        return publicKey;
099    }
100
101    /**
102     * <pre>
103     * POPOSigningKeyInput ::= SEQUENCE {
104     *        authInfo             CHOICE {
105     *                                 sender              [0] GeneralName,
106     *                                 -- used only if an authenticated identity has been
107     *                                 -- established for the sender (e.g., a DN from a
108     *                                 -- previously-issued and currently-valid certificate
109     *                                 publicKeyMAC        PKMACValue },
110     *                                 -- used if no authenticated GeneralName currently exists for
111     *                                 -- the sender; publicKeyMAC contains a password-based MAC
112     *                                 -- on the DER-encoded value of publicKey
113     *        publicKey           SubjectPublicKeyInfo }  -- from CertTemplate
114     * </pre>
115     * @return a basic ASN.1 object representation.
116     */
117    public ASN1Primitive toASN1Primitive()
118    {
119        ASN1EncodableVector v = new ASN1EncodableVector();
120
121        if (sender != null)
122        {
123            v.add(new DERTaggedObject(false, 0, sender));
124        }
125        else
126        {
127            v.add(publicKeyMAC);
128        }
129
130        v.add(publicKey);
131
132        return new DERSequence(v);
133    }
134}