001package org.apache.commons.ssl.org.bouncycastle.asn1.ua;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
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;
011
012public class DSTU4145Params
013    extends ASN1Object
014{
015    private static final byte DEFAULT_DKE[] = {
016        (byte)0xa9, (byte)0xd6, (byte)0xeb, 0x45, (byte)0xf1, 0x3c, 0x70, (byte)0x82,
017        (byte)0x80, (byte)0xc4, (byte)0x96, 0x7b, 0x23, 0x1f, 0x5e, (byte)0xad,
018        (byte)0xf6, 0x58, (byte)0xeb, (byte)0xa4, (byte)0xc0, 0x37, 0x29, 0x1d,
019        0x38, (byte)0xd9, 0x6b, (byte)0xf0, 0x25, (byte)0xca, 0x4e, 0x17,
020        (byte)0xf8, (byte)0xe9, 0x72, 0x0d, (byte)0xc6, 0x15, (byte)0xb4, 0x3a,
021        0x28, (byte)0x97, 0x5f, 0x0b, (byte)0xc1, (byte)0xde, (byte)0xa3, 0x64,
022        0x38, (byte)0xb5, 0x64, (byte)0xea, 0x2c, 0x17, (byte)0x9f, (byte)0xd0,
023        0x12, 0x3e, 0x6d, (byte)0xb8, (byte)0xfa, (byte)0xc5, 0x79, 0x04};
024
025
026    private ASN1ObjectIdentifier namedCurve;
027    private DSTU4145ECBinary ecbinary;
028    private byte[] dke = DEFAULT_DKE;
029
030    public DSTU4145Params(ASN1ObjectIdentifier namedCurve)
031    {
032        this.namedCurve = namedCurve;
033    }
034
035    public DSTU4145Params(DSTU4145ECBinary ecbinary)
036    {
037        this.ecbinary = ecbinary;
038    }
039
040    public boolean isNamedCurve()
041    {
042        return namedCurve != null;
043    }
044
045    public DSTU4145ECBinary getECBinary()
046    {
047        return ecbinary;
048    }
049
050    public byte[] getDKE()
051    {
052        return dke;
053    }
054
055    public static byte[] getDefaultDKE()
056    {
057        return DEFAULT_DKE;
058    }
059
060    public ASN1ObjectIdentifier getNamedCurve()
061    {
062        return namedCurve;
063    }
064
065    public static DSTU4145Params getInstance(Object obj)
066    {
067        if (obj instanceof DSTU4145Params)
068        {
069            return (DSTU4145Params)obj;
070        }
071
072        if (obj != null)
073        {
074            ASN1Sequence seq = ASN1Sequence.getInstance(obj);
075            DSTU4145Params params;
076
077            if (seq.getObjectAt(0) instanceof ASN1ObjectIdentifier)
078            {
079                params = new DSTU4145Params(ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0)));
080            }
081            else
082            {
083                params = new DSTU4145Params(DSTU4145ECBinary.getInstance(seq.getObjectAt(0)));
084            }
085
086            if (seq.size() == 2)
087            {
088                params.dke = ASN1OctetString.getInstance(seq.getObjectAt(1)).getOctets();
089                if (params.dke.length != DSTU4145Params.DEFAULT_DKE.length)
090                {
091                    throw new IllegalArgumentException("object parse error");
092                }
093            }
094
095            return params;
096        }
097
098        throw new IllegalArgumentException("object parse error");
099    }
100
101    public ASN1Primitive toASN1Primitive()
102    {
103        ASN1EncodableVector v = new ASN1EncodableVector();
104
105        if (namedCurve != null)
106        {
107            v.add(namedCurve);
108        }
109        else
110        {
111            v.add(ecbinary);
112        }
113
114        if (!org.bouncycastle.util.Arrays.areEqual(dke, DEFAULT_DKE))
115        {
116            v.add(new DEROctetString(dke));
117        }
118
119        return new DERSequence(v);
120    }
121}