001package org.apache.commons.ssl.org.bouncycastle.asn1.eac;
002
003import java.io.IOException;
004import java.util.Enumeration;
005
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ParsingException;
010import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
011import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
012import org.apache.commons.ssl.org.bouncycastle.asn1.BERTags;
013import org.apache.commons.ssl.org.bouncycastle.asn1.DERApplicationSpecific;
014import org.apache.commons.ssl.org.bouncycastle.asn1.DEROctetString;
015
016//import java.math.BigInteger;
017
018
019public class CVCertificateRequest
020    extends ASN1Object
021{
022    private CertificateBody certificateBody;
023
024    private byte[] innerSignature = null;
025    private byte[] outerSignature = null;
026
027    private int valid;
028
029    private static int bodyValid = 0x01;
030    private static int signValid = 0x02;
031
032    private CVCertificateRequest(DERApplicationSpecific request)
033        throws IOException
034    {
035        if (request.getApplicationTag() == EACTags.AUTHENTIFICATION_DATA)
036        {
037            ASN1Sequence seq = ASN1Sequence.getInstance(request.getObject(BERTags.SEQUENCE));
038
039            initCertBody(DERApplicationSpecific.getInstance(seq.getObjectAt(0)));
040
041            outerSignature = DERApplicationSpecific.getInstance(seq.getObjectAt(seq.size() - 1)).getContents();
042        }
043        else
044        {
045            initCertBody(request);
046        }
047    }
048
049    private void initCertBody(DERApplicationSpecific request)
050        throws IOException
051    {
052        if (request.getApplicationTag() == EACTags.CARDHOLDER_CERTIFICATE)
053        {
054            ASN1Sequence seq = ASN1Sequence.getInstance(request.getObject(BERTags.SEQUENCE));
055            for (Enumeration en = seq.getObjects(); en.hasMoreElements();)
056            {
057                DERApplicationSpecific obj = DERApplicationSpecific.getInstance(en.nextElement());
058                switch (obj.getApplicationTag())
059                {
060                case EACTags.CERTIFICATE_CONTENT_TEMPLATE:
061                    certificateBody = CertificateBody.getInstance(obj);
062                    valid |= bodyValid;
063                    break;
064                case EACTags.STATIC_INTERNAL_AUTHENTIFICATION_ONE_STEP:
065                    innerSignature = obj.getContents();
066                    valid |= signValid;
067                    break;
068                default:
069                    throw new IOException("Invalid tag, not an CV Certificate Request element:" + obj.getApplicationTag());
070                }
071            }
072        }
073        else
074        {
075            throw new IOException("not a CARDHOLDER_CERTIFICATE in request:" + request.getApplicationTag());
076        }
077    }
078
079    public static CVCertificateRequest getInstance(Object obj)
080    {
081        if (obj instanceof CVCertificateRequest)
082        {
083            return (CVCertificateRequest)obj;
084        }
085        else if (obj != null)
086        {
087            try
088            {
089                return new CVCertificateRequest(DERApplicationSpecific.getInstance(obj));
090            }
091            catch (IOException e)
092            {
093                throw new ASN1ParsingException("unable to parse data: " + e.getMessage(), e);
094            }
095        }
096
097        return null;
098    }
099
100    ASN1ObjectIdentifier signOid = null;
101    ASN1ObjectIdentifier keyOid = null;
102
103    public static byte[] ZeroArray = new byte[]{0};
104
105
106    String strCertificateHolderReference;
107
108    byte[] encodedAuthorityReference;
109
110    int ProfileId;
111
112    /**
113     * Returns the body of the certificate template
114     *
115     * @return the body.
116     */
117    public CertificateBody getCertificateBody()
118    {
119        return certificateBody;
120    }
121
122    /**
123     * Return the public key data object carried in the request
124     * @return  the public key
125     */
126    public PublicKeyDataObject getPublicKey()
127    {
128        return certificateBody.getPublicKey();
129    }
130
131    public byte[] getInnerSignature()
132    {
133        return innerSignature;
134    }
135
136    public byte[] getOuterSignature()
137    {
138        return outerSignature;
139    }
140
141    byte[] certificate = null;
142    protected String overSignerReference = null;
143
144    public boolean hasOuterSignature()
145    {
146        return outerSignature != null;
147    }
148
149    byte[] encoded;
150
151    PublicKeyDataObject iso7816PubKey = null;
152
153    public ASN1Primitive toASN1Primitive()
154    {
155        ASN1EncodableVector v = new ASN1EncodableVector();
156
157        v.add(certificateBody);
158
159        try
160        {
161            v.add(new DERApplicationSpecific(false, EACTags.STATIC_INTERNAL_AUTHENTIFICATION_ONE_STEP, new DEROctetString(innerSignature)));
162        }
163        catch (IOException e)
164        {
165            throw new IllegalStateException("unable to convert signature!");
166        }
167
168        return new DERApplicationSpecific(EACTags.CARDHOLDER_CERTIFICATE, v);
169    }
170}