1package org.bouncycastle.jcajce.provider.asymmetric.dh;
2
3import java.io.IOException;
4import java.security.spec.AlgorithmParameterSpec;
5import java.security.spec.InvalidParameterSpecException;
6
7import javax.crypto.spec.DHParameterSpec;
8
9import org.bouncycastle.asn1.ASN1Encoding;
10import org.bouncycastle.asn1.pkcs.DHParameter;
11
12public class AlgorithmParametersSpi
13    extends java.security.AlgorithmParametersSpi
14{
15    DHParameterSpec     currentSpec;
16
17    protected boolean isASN1FormatString(String format)
18    {
19        return format == null || format.equals("ASN.1");
20    }
21
22    protected AlgorithmParameterSpec engineGetParameterSpec(
23        Class paramSpec)
24        throws InvalidParameterSpecException
25    {
26        if (paramSpec == null)
27        {
28            throw new NullPointerException("argument to getParameterSpec must not be null");
29        }
30
31        return localEngineGetParameterSpec(paramSpec);
32    }
33
34
35
36
37        /**
38         * Return the PKCS#3 ASN.1 structure DHParameter.
39         * <p>
40         * <pre>
41         *  DHParameter ::= SEQUENCE {
42         *                   prime INTEGER, -- p
43         *                   base INTEGER, -- g
44         *                   privateValueLength INTEGER OPTIONAL}
45         * </pre>
46         */
47        protected byte[] engineGetEncoded()
48        {
49            DHParameter dhP = new DHParameter(currentSpec.getP(), currentSpec.getG(), currentSpec.getL());
50
51            try
52            {
53                return dhP.getEncoded(ASN1Encoding.DER);
54            }
55            catch (IOException e)
56            {
57                throw new RuntimeException("Error encoding DHParameters");
58            }
59        }
60
61        protected byte[] engineGetEncoded(
62            String format)
63        {
64            if (isASN1FormatString(format))
65            {
66                return engineGetEncoded();
67            }
68
69            return null;
70        }
71
72        protected AlgorithmParameterSpec localEngineGetParameterSpec(
73            Class paramSpec)
74            throws InvalidParameterSpecException
75        {
76            if (paramSpec == DHParameterSpec.class)
77            {
78                return currentSpec;
79            }
80
81            throw new InvalidParameterSpecException("unknown parameter spec passed to DH parameters object.");
82        }
83
84        protected void engineInit(
85            AlgorithmParameterSpec paramSpec)
86            throws InvalidParameterSpecException
87        {
88            if (!(paramSpec instanceof DHParameterSpec))
89            {
90                throw new InvalidParameterSpecException("DHParameterSpec required to initialise a Diffie-Hellman algorithm parameters object");
91            }
92
93            this.currentSpec = (DHParameterSpec)paramSpec;
94        }
95
96        protected void engineInit(
97            byte[] params)
98            throws IOException
99        {
100            try
101            {
102                DHParameter dhP = DHParameter.getInstance(params);
103
104                if (dhP.getL() != null)
105                {
106                    currentSpec = new DHParameterSpec(dhP.getP(), dhP.getG(), dhP.getL().intValue());
107                }
108                else
109                {
110                    currentSpec = new DHParameterSpec(dhP.getP(), dhP.getG());
111                }
112            }
113            catch (ClassCastException e)
114            {
115                throw new IOException("Not a valid DH Parameter encoding.");
116            }
117            catch (ArrayIndexOutOfBoundsException e)
118            {
119                throw new IOException("Not a valid DH Parameter encoding.");
120            }
121        }
122
123        protected void engineInit(
124            byte[] params,
125            String format)
126            throws IOException
127        {
128            if (isASN1FormatString(format))
129            {
130                engineInit(params);
131            }
132            else
133            {
134                throw new IOException("Unknown parameter format " + format);
135            }
136        }
137
138        protected String engineToString()
139        {
140            return "Diffie-Hellman Parameters";
141        }
142}
143