AlgorithmParametersSpi.java revision 53b61f9fe9d58034fcc7021137e92460f91b70ce
1package org.bouncycastle.jcajce.provider.asymmetric.dsa;
2
3import java.io.IOException;
4import java.security.spec.AlgorithmParameterSpec;
5import java.security.spec.DSAParameterSpec;
6import java.security.spec.InvalidParameterSpecException;
7
8import org.bouncycastle.asn1.ASN1Encoding;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.x509.DSAParameter;
11
12public class AlgorithmParametersSpi
13    extends java.security.AlgorithmParametersSpi
14{
15    DSAParameterSpec 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     * Return the X.509 ASN.1 structure DSAParameter.
36     * <pre>
37     *  DSAParameter ::= SEQUENCE {
38     *                   prime INTEGER, -- p
39     *                   subprime INTEGER, -- q
40     *                   base INTEGER, -- g}
41     * </pre>
42     */
43    protected byte[] engineGetEncoded()
44    {
45        DSAParameter dsaP = new DSAParameter(currentSpec.getP(), currentSpec.getQ(), currentSpec.getG());
46
47        try
48        {
49            return dsaP.getEncoded(ASN1Encoding.DER);
50        }
51        catch (IOException e)
52        {
53            throw new RuntimeException("Error encoding DSAParameters");
54        }
55    }
56
57    protected byte[] engineGetEncoded(
58        String format)
59    {
60        if (isASN1FormatString(format))
61        {
62            return engineGetEncoded();
63        }
64
65        return null;
66    }
67
68    protected AlgorithmParameterSpec localEngineGetParameterSpec(
69        Class paramSpec)
70        throws InvalidParameterSpecException
71    {
72        if (paramSpec == DSAParameterSpec.class)
73        {
74            return currentSpec;
75        }
76
77        throw new InvalidParameterSpecException("unknown parameter spec passed to DSA parameters object.");
78    }
79
80    protected void engineInit(
81        AlgorithmParameterSpec paramSpec)
82        throws InvalidParameterSpecException
83    {
84        if (!(paramSpec instanceof DSAParameterSpec))
85        {
86            throw new InvalidParameterSpecException("DSAParameterSpec required to initialise a DSA algorithm parameters object");
87        }
88
89        this.currentSpec = (DSAParameterSpec)paramSpec;
90    }
91
92    protected void engineInit(
93        byte[] params)
94        throws IOException
95    {
96        try
97        {
98            DSAParameter dsaP = DSAParameter.getInstance(ASN1Primitive.fromByteArray(params));
99
100            currentSpec = new DSAParameterSpec(dsaP.getP(), dsaP.getQ(), dsaP.getG());
101        }
102        catch (ClassCastException e)
103        {
104            throw new IOException("Not a valid DSA Parameter encoding.");
105        }
106        catch (ArrayIndexOutOfBoundsException e)
107        {
108            throw new IOException("Not a valid DSA Parameter encoding.");
109        }
110    }
111
112    protected void engineInit(
113        byte[] params,
114        String format)
115        throws IOException
116    {
117        if (isASN1FormatString(format) || format.equalsIgnoreCase("X.509"))
118        {
119            engineInit(params);
120        }
121        else
122        {
123            throw new IOException("Unknown parameter format " + format);
124        }
125    }
126
127    protected String engineToString()
128    {
129        return "DSA Parameters";
130    }
131}
132